Experience an access violation if you access the STL object through a pointer or reference in a different DLL or Exe

Source: Internet
Author: User
Tags static class knowledge base

Symptomswhen accessing an STL object created in one DLL or EXE through a pointer or reference in a different DLL or EXE, y OU may experience a access violation or other serious program errors including the appearance of data corruption or data Loss. Causemost classes in the standard C + + Libraries use static data members directly or indirectly. Since These classes is generated through template instantiation, each executable image (usually with DLL or EXE file name Extensions) would contain its own copy of the static data member for a given class. When a method of the class that requires the static data member was executed, it uses the static data member in the Executa ble image in which the method code resides. Since the static data members in the executable images is not on sync, this action could result in an access violation or Data may appear to be lost or corrupted. Resolution
  1. Export accessor methods from the executable image that created the STL object. These methods wrap the required functionality of the STL object. The This is the STL object would only be directly accessed inside a single executable image. For example, suppose MyProgram.EXE needs to get the next element in deque<myclass> that resides in MyLibrary.DLL. MyLibrary.DLL could export an accessor method, myclass* Dequenextitem (/*...*/). Then MyProgram.EXE could execute this method to get the next item in the Deque. See the code sample below for a more complete example.

    This option works with STL objects that is either global, static, or static data members of a class that is not E Xported from a DLL. This option would not be work for non-static data members of a class that is exported from a DLL or for automatic data.
  2. Export the template class instantiation from one executable image and import it to the other executable images. For example, if MyLibrary.DLL passes a pointer to vector<myclass> back to a function in MyProgram.EXE and then export t He classes MyClass and vector<myclass> from MyLibrary.DLL. Then import these classes into MyProgram.EXE. By doing this, you'll have one copy of the static class members residing in MyLibrary.DLL. For more information about exporting and importing STL, click the following article number to view the article in the MICR Osoft Knowledge Base:

    168958 How to export STL components inside and outside of a class

Statusthis behavior is by design. More informationsteps to reproduce the behavior
//---------------------------------------------------------
Avexe. Cpp
Compile Options Needed:/GX
#pragma warning (disable:4786)
#include <map>
#include <string>
#include <stdio.h>

__declspec (dllimport)
std::map<int,std::string>* Givemeamap (int n);

__declspec (dllimport)
void Showmethemap (std::map<int,std::string> *amap);

__declspec (dllexport)
Const char* MAPITEMX (std::map<int,std::string> *m, int x);

int main () {

Create the map in the DLL
int x = 6;
std::map<int,std::string> *p = Givemeamap (x);

Display the contents of the map from the DLL
printf ("Showing contents from the dll\n");
Showmethemap (P);

Display the contents of the map from the EXE
Using the accessor function from the DLL so we
Aren ' t directly accessing the map
printf ("Showing contents from the EXE using accessor\n");
int i = x;
while (i--) {
printf ("%d =%s\n", I,mapitemx (P,i));
}

Access violation when accessing the map that
is created in the DLL from the EXE
printf ("Showing contents from the EXE directly\n");
while (x--) {
printf ("%d =%s\n", x, (*p) [X].c_str ());
}

return 0;
}

//---------------------------------------------------------
Avdll. Cpp
Compile Options NEEDED/GX
#pragma warning (disable:4786)
#include <map>
#include <string>
#include <stdlib.h>

Create the map here in the DLL
__declspec (dllexport)
std::map<int,std::string>* Givemeamap (int n) {
std::map<int,std::string> *m = new std::map<int,std::string>;
while (n--) {
Char b[33];
Itoa (n,b,2);
(*m) [N] = std::string (b);
}
return m;
}

We can access the map without error from the executable
Image where the map was created
__declspec (dllexport)
void Showmethemap (std::map<int,std::string> *p) {
int x = P->size ();
while (x--) {
printf ("%d =%s\n", x, (*p) [X].c_str ());
}
}

An accessor method to return the associated C string
For key X
__declspec (dllexport)
Const char* MAPITEMX (std::map<int,std::string> *m, int x) {
return (*M) [X].c_str ();
}
Properties

Article Id:172396-last review:sep 2, 2005-revision:1

Experience an access violation if you access the STL object through a pointer or reference in a different DLL or Exe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.