Use auto_ptr in VC6 dll cause crash
- by Yan Cheng CHEOK
// dll
#include <memory>
__declspec(dllexport) std::auto_ptr<int> get();
__declspec(dllexport) std::auto_ptr<int> get()
{
return std::auto_ptr<int>(new int());
}
// exe
#include <iostream>
#include <memory>
__declspec(dllimport) std::auto_ptr<int> get();
int main() {
{
std::auto_ptr<int> x = get();
}
std::cout << "done\n";
getchar();
}
The following code run perfectly OK under VC9. However, under VC6, I will experience an immediate crash with the following message.
Debug Assertion Failed!
Program:
C:\Projects\use_dynamic_link\Debug\use_dynamic_link.exe
File: dbgheap.c Line: 1044
Expression:
_CrtIsValidHeapPointer(pUserData)
Is it exporting auto_ptr under VC6 is not allowed?
It is a known problem that exporting STL collection classes through DLL.
http://stackoverflow.com/questions/2451714/access-violation-when-accessing-an-stl-object-through-a-pointer-or-reference-in-a
However, I Google around and do not see anything mention for std::auto_ptr.
Any workaround?