Question

Does the stack get unwound (destructors run) when a SIGABRT occurs in C++?

Thanks.

Was it helpful?

Solution

This answer indicates that destructors aren't called.

OTHER TIPS

No:

$ cat test.cc
#include <iostream>
#include <sys/types.h>
#include <signal.h>

class Test {
public:
   ~Test() { std::cout << "~Test called" << std::endl; }
};

int main(int argc, char *argv[])
{
   Test t = Test();
   if (argc > 1) {
      kill(0, SIGABRT);
   }
   return 0;
}
$ g++ test.cc
$ ./a.out
~Test called
$ ./a.out 1
Aborted

No, only exceptions trigger stack unwinding. Signals are part of POSIX, which is a C API, so it's not "aware of" C++ facilities such as exceptions.

The signal(3) man page on my Mac OS X box says

  No    Name         Default Action       Description
...
 6     SIGABRT      create core image    abort program (formerly SIGIOT)

which suggests to me that the default is to not unwind...

the signal SIGABRT is used for making core file of running application some time. We some time use this signal to debug application. And as far as I know Destructors are not called by this signal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top