GNOME Bugzilla – Bug 99558
g_atexit() doesn't work on Win32
Last modified: 2004-12-22 21:47:04 UTC
For obscure reasons g_atexit() doesn't work in GLib on Windows, if GLib is built as a DLL, and compiled with gcc 2.95.3. If GLib is built with MSVC, it presumably works (a similar minimal test case behaves thusly). I haven't yet checked yet whether GCC 3.2 would work any better. The minimal test case mentioned has a DLL that contains a function that calls atexit(), i.e. the same situation as with g_atexit() in the GLib DLL. If you build that DLL with MSVC, the atexit() call works. If you build it with gcc, it doesn't. Anyway, calling atexit() directly from the application code works both with gcc and MSVC. The simplest fix would be to have in gutils.h: #ifdef G_OS_WIN32 #define g_atexit(func) atexit(func) #else ... and perhaps add something like this to g_atexit(): #if defined(G_OS_WIN32) && defined(__GNUC__) g_warning ("This implementation of g_atexit() doesn't work. Recompile your code with at least GLib version 2.0.10") (or 2.1.x?) #endif A more elaborate fix would be to check how exactly the atexit() that one gets when building with gcc differes from the one one gets when building with MSVC, and add appropriate workarounds. Unfortunately this requires looking at the source code to Microsoft's C runtime. The source code is downloadable as part of the Windows Platform SDK, but I am not a lawyer, I can't say to what extent the Platform SDK license allows you to make use of knowledge acquired by reading the source code.
Building the GLib DLL with a newer mingw runtime fixed this. The next binary distribution of GLib will be built with GCC 3.2 and the newer mingw runtime. Until then, use atexit() directly. Closing, as this was more of a bug in mingw than in GLib.