You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE.
[Http://support.microsoft.com/default.aspx? SCID = kb % 3ben-us % 3b172396]
On this page
* Symptoms
* Cause
* Resolution
* Status
* More Information
O steps to reproduce the behavior
Symptoms
When accessing an STL object created in one DLL or EXE through a pointer or reference in a different DLL or EXE, you may experience an access violation or other serious program errors including the appearance of data upload uption or data loss.
Cause
Most classes in the Standard C ++ libraries use static data members directly or indirectly. since these classes are generated through template instantiation, each executable image (usually with DLL or EXE file name extensions) will contain in its own copy of the static data member for a given class. when a method of the class that requires the static data member is executed, it uses the static data member in the executable image in which the method code resides. since the static data members in the executable images are not in sync, this action cocould result in an access violation or data may appear to be lost or upted.
Resolution
1. export accessor methods from the executable image that created the STL object. these methods wrap the required functionality of the STL object. in this way, the STL object will 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 cocould export an accessor method, myclass * dequenextitem (/*... */). then myprogram. EXE cocould execute this method to get the next item in the deque. see the code sample below for a more complete example.
This option works for STL objects that are either global, static, or static data members of a class that are not exported from a DLL. this option will not work for non-static data members of a class that are exported from a DLL or for automatic data.
2. export the template class instantiation from one executable image and import it into the other executable images. for example, if mylibrary. DLL passes a pointer to vector <myclass> back to a function in myprogram. EXE, then export the classes myclass and vector <myclass> from mylibrary. DLL. then import these classes into myprogram. EXE. by doing this, you will 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 Microsoft Knowledge Base:
(EN) http://support.microsoft.com/default.aspx? SCID = kb % 3ben-us % 3b168958
Http://support.microsoft.com/kb/168958/ (CH)
About: How to export STL components inside and outside of a class
Status
This behavior is by design.
More information
Steps 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
// Was 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 ();
}