g_atexit

Tor Lillqvist <[email protected]>
Newsgroups gmane.comp.video.gimp.windows.devel,gmane.comp.gnu.mingw.user
Message-ID <[email protected]>
Richard Warren writes:
 > Has anybody used GLib's g_atexit() function in a Windows
 > application?

Hmm. Apparently not, or at least nobody (else) has noticed that it
doesn't work, and reported it.

After digging into this a bit, it seems the cause of the behaviour
must be rather complicated. I expanded your test program a bit and
also wrote a minimal DLL (produced from atexitdll.c below) that
contains a function that calls atexit(), i.e. in principle does what
g_atexit() does.

The funny thing is that when atexitdll.dll is built with gcc, the
atexit handler installed with callatexit() is not called. (Which
corresponds to how g_atexit() now works, i.e. doesn't work. The GLib
DLL I distribute is built with gcc.)

But if atexitdll.dll is built with MSVC, the atexit handler is
called. Hmmm....

Anyway, for now, the easiest workaround is to call atexit() in your
app instead of g_atexit(). (The reason why there is the g_atexit()
wrapper in GLib is to handle some systems where there is no atexit()
but some other function, like on_exit(), instead. If your app is
intended for the majority of systems (like Windows or most Unixes),
you can use the normal atexit().)

I'll check if the atexit() implementation in GCC 3.2 works
differently. (I still have been using GCC 2.95.3.)

Check Bugzilla for followups
http://bugzilla.gnome.org/show_bug.cgi?id=99558 .

--tml

atexit.c:
================
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

extern void __declspec(dllimport) callatexit(void (*func) ());

static void exitFunc(void)
{
    printf("This is exitFunc().\n");
}

static void exitFunc2(void)
{
    printf("This is  exitFunc2().\n");
}

static void exitFunc3(void)
{
    printf("This is  exitFunc3().\n");
}

int main(int argc, char *argv[])
{
    printf("Installing function to call on exit.\n");

    atexit (exitFunc2);
    g_atexit(exitFunc);
    callatexit (exitFunc3);

    printf("Exiting.\n");
    exit(0);
}
================

atexitdll.c:
================
#include <stdio.h>
#include <stdlib.h>

int __declspec (dllexport)
callatexit(void (*func) (void))
{
    printf("in callatexit.\n");

    return atexit(func);
}
================



To Post a message, send it to:   [email protected]
To Unsubscribe, send a blank message to: [email protected] 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.