Debug assertion failed: _CrtIsValidHeapPointer
Hi all,
My main application is calling a method of class which is implemented in a dll. I have the possibility to change both and after a bit of googling I was able to sort out my problem. It appears that when I pass a std::wstring by value to the dll I get the assertion, but when I pass it by reference everything works fine. I get the assertion at the moment the dll method returns.
I would like to understand why this is caused.
My understanding is that when I pass a string by value the copy constructor is called to create a local instance in the application heap. When the method returns it tries to delete this local instance but it is not in the dll heap and as such this fails with the assertion.
Can somebody confirm this or explain why this is happening?
thank you,
Jef
Re: Debug assertion failed: _CrtIsValidHeapPointer
It is dangerous to potentially allow heap allocations done in a DLL to be released outside the DLL, or vice versa.
In fact, it's a bit iffy to even pass a C++ class like std::wstring over a DLL boundary by reference----what if the DLL was built against a different version of the STL, and std::wstring had a different structure there?
In general, you should limit the data passing over a DLL boundary to primitive types.
Re: Debug assertion failed: _CrtIsValidHeapPointer
In addition to Lindley's post, passing by reference may also create problems:
http://support.microsoft.com/kb/172396
There are a lot of articles about this problem. Avoid using STL classes in Dll interface. Use plain pointers instead of std::wstring.
Re: Debug assertion failed: _CrtIsValidHeapPointer
That is probably DLL and EXE are using different versions of runtime library. Or one is using Debug build or another is using Retail/Release build. Make both of them consistent. Use Dependency Walker to find out what Runtime DLLs are being used.