Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC destructor behaviour

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");
}
like image 529
joveha Avatar asked May 25 '10 17:05

joveha


1 Answers

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.

like image 194
Yann Ramin Avatar answered Oct 04 '22 03:10

Yann Ramin