You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE (172396)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q172396 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 corruption
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 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- 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 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 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. - 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:
168958
How to export STL components inside and outside of a class
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 9/2/2005 |
---|
Keywords: | kbtshoot kbCRT kbprb KB172396 kbAudDeveloper |
---|
|