开发者

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 proc开发者_如何学Goess 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");
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜