Since GCC 4.5, I get crashes on FreeLibrary() freeing a DLL making use of C++ std::string.
The problem only happens when invoking the linker by 'gcc' rather than 'g++'.
However, both executable and shared library are built by explicitly linking: -lstdc++
The following error message will show to a console or popup box right after the FreeLibrary() call:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Very easy to reproduce:
DLL code:
-------------------------
#include <string>
void Test()
{
std::string("hello");
}
--------------------------
gcc -g -Wall -D_DEBUG -c cppTest.cpp -o obj/debug.win32/cppTest.o
gcc obj/debug.win32/cppTest.o -shared -lstdc++ -o obj/debug.win32/cppTest.dll
(If using g++ instead of gcc here, no problem)
Test app code:
-------------------------
#include <windows.h>
int main()
{
HMODULE l = LoadLibrary("cppTest");
FreeLibrary(l);
return 0;
}
-------------------------
gcc -g -Wall -D_DEBUG -c cppTestClient.cpp -o obj/debug.win32/cppTestClient.o
gcc obj/debug.win32/cppTestClient.o -mwindows -lstdc++ -o obj/debug.win32/cppTestClient.exe
Thanks,
Jerome
All I can say is that you're supposed to be using G++ instead of GCC for C++ compilation. If you believe this is truly an issue then take it up with the bugs list for GCC.
Turns out that adding -shared-libgcc to the gcc linker line works fine.
(Thanks to apinski on #gcc)
Arguably, this should be default when linking with -lstdc++ dynamically.
Taking it up with them. Thanks Earnie.