PRB: Access Violation When Accessing STL Object in DLLLast reviewed: August 18, 1997Article ID: Q172396 |
The information in this article applies to:
SYMPTOMSWhen 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 corruption or data loss.
CAUSEMost 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 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 could result in an access violation or data may appear to be lost or corrupted.
RESOLUTION
STATUSThis behavior is by design.
MORE INFORMATION
Steps to Reproduce 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(); } Keywords : CRTIss STLIss Version : WINDOWS NT:5.0 Platform : NT WINDOWS Issue type : kbprb |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |