I had some problem with inter-module memory allocation / deallocation. It seems like this post describes the same error.
Here is a piece of code:
My main application:
#pragma comment(lib, "mydll.lib")
__declspec(dllimport) std::shared_ptr<VOID> GetMemory(size_t size);
int wmain(int argc, wchar_t* argv[])
{
std::shared_ptr<VOID> lpMem = GetMemory(100);
return 0;
}
Dll code:
__declspec(dllexport) std::shared_ptr<VOID> GetMemory(size_t size);
BOOL WINAPI DllMain(HINSTANCE, DWORD dwReason, LPVOID)
{
return TRUE;
}
std::shared_ptr<VOID> GetMemory(size_t size)
{
return std::shared_ptr<VOID>(new (std::nothrow) char[size]);
}
Either /MT
or /MD
compiler flags are processed correctly. And execution does not fail.
My question is: why does the solution with std::shared_ptr
work properly? What does it change? And what are those "memory managers" (mentioned in the thread linked above), which are different within a single process? Is that just an CRT abstraction? Or, may be CRT provides some specific implementation of memory allocations?
I think, any call of new
/ malloc
/ LocalAlloc
leads to the HeapAlloc
. Am I right? If so, why new
/delete
-calls (not wrapped with the std::shared_ptr
) in different modules within a single process lead to crash?