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");
}