GCC destructor behaviour

Posted by joveha on Stack Overflow See other posts from Stack Overflow or by joveha
Published on 2010-05-25T17:55:57Z Indexed on 2010/05/25 18:01 UTC
Read the original article Hit count: 316

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about c

    Related posts about gcc