views:

107

answers:

1

I've noticed a difference in behaviour for gcc's destructor when compiled under linux and crosscompiled with mingw.

On linux the destructor will not get called unless the program terminates normally by itself (returns from main). I guess that kind of makes sense if you take signal handlers into account.

On Win32 however, the destructor is called if the program is terminated by say a CTRL-C, but not when killed from the Task Manager.

Why is this? And what would you suggest to make the destructor get called no matter how the process terminates - on Win32 in particular?

Example code:

#include <stdio.h>

int main(int argc, char **argv) {
        printf("main\n");
        while(1) {}
    return 0;
}

__attribute__((destructor)) static void mydestructor(void) {
        printf("destructor\n");
}
+2  A: 

A kill from the process manager on Window is analogous to a kill -9 on Linux - the program is not given the opportunity to clean up.

Yann Ramin
+1, just as I mentioned in my comment on the original post.
Carl Norum
I see. And on system shutdown I presume cleanup will be possible since it won't be a hard 'kill -9'?
joveha
@joveha, that's probably correct, yes.
Carl Norum
If you need stop/start signals, I'd advise hooking into the Windows service framework, or inittab on Linux.
Yann Ramin