Re: _CrtIsValidHeapPointer problem while exporting dll vc6.
dipukurian@gmail.com wrote:
Many thanks to both of u..
I understand that when there are two different heap
managers and the mem. allocated by one manager is tried to be freed by
the other one, the prob. occurs.
But i need one more clarification.
the stl string is created by the dll in
its own heap. it is freed when the destructor is called. the code of
the destructor is residing in the dll.
When you statically link to the CRT, you rely on template instantiation
of std::string's members to decide where the code is generated
(std::string is slightly special, since std::string and std::wstring are
exported from the CRT DLL. This doesn't apply to you though, since you
are statically linking). The code for the destructor will lie wherever
the destructor is called - in the exe, in the dll, or even in both,
since an implicit instantiation of the destructor will be generated
there. The version called will be the one for the module in which the
string is destroyed. You could get around this by exporting the explicit
instantiation std::string from your DLL, in the same way that the CRT
DLL does it, something like:
//library.h:
#include <string>
namespace std
{
template class __declspec(dllimport) String_val<char, allocator<char> >;
template class __declspec(dllimport) basic_string<char,
char_traits<char>, allocator<char> >;
}
//library.cpp (don't include .h)
#include <string>
namespace std
{
template class __declspec(dllexport) String_val<char, allocator<char> >;
template class __declspec(dllexport) basic_string<char,
char_traits<char>, allocator<char> >;
}
You may need to import/export std::allocator<char> too. Obviously this
isn't portable between compiler versions, and linking to the CRT DLL is
a simpler, superior solution.
Tom