Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is destructor called if SIGINT or SIGSTP issued?

I have a class with a user-defined destructor. If the class was instantiated initially, and then SIGINT is issued (using CTRL+C in unix) while the program is running, will the destructor be called? What is the behaviour for SIGSTP (CTRL + Z in unix)?

like image 970
SkypeMeSM Avatar asked Nov 22 '10 20:11

SkypeMeSM


People also ask

Are destructors called on Sigint?

Show activity on this post. If you do not handle these signals yourself, then, no, the destructors are not called. However, the operating system will reclaim any resources your program used when it terminates. If you wish to handle signals yourself, then consider checking out the sigaction standard library function.

Is the destructor automatically called?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ).

How do you make sure a destructor is called?

the destructor is called when code is execused to line "finish". If the object is created via a pointer(for example,A * a2 = new A();),the destructor is called when the pointer is deleted(delete a2;).

What calls a destructor?

An explicit call to destructor is only necessary when an object is placed at a particular location in memory by using placement new. Destructor should not be called explicitly when the object is dynamically allocated because the delete operator automatically calls destructor.


2 Answers

No, by default, most signals cause an immediate, abnormal exit of your program.

However, you can easily change the default behavior for most signals.

This code shows how to make a signal exit your program normally, including calling all the usual destructors:

#include <iostream> #include <signal.h> #include <unistd.h> #include <cstring> #include <atomic>  std::atomic<bool> quit(false);    // signal flag  void got_signal(int) {     quit.store(true); }  class Foo { public:     ~Foo() { std::cout << "destructor\n"; } };  int main(void) {     struct sigaction sa;     memset( &sa, 0, sizeof(sa) );     sa.sa_handler = got_signal;     sigfillset(&sa.sa_mask);     sigaction(SIGINT,&sa,NULL);      Foo foo;    // needs destruction before exit     while (true)     {         // do real work here...         sleep(1);         if( quit.load() ) break;    // exit normally after SIGINT     }     return 0; } 

If you run this program and press control-C, you should see the word "destructor" printed. Be aware that your signal handler functions (got_signal) should rarely do any work, other than setting a flag and returning quietly, unless you really know what you are doing.

Most signals are catchable as shown above, but not SIGKILL, you have no control over it because SIGKILL is a last-ditch method for killing a runaway process, and not SIGSTOP which allows a user to freeze a process cold. Note that you can catch SIGTSTP (control-Z) if desired, but you don't need to if your only interest in signals is destructor behavior, because eventually after a control-Z the process will be woken up, will continue running, and will exit normally with all the destructors in effect.

like image 141
corecursion Avatar answered Sep 30 '22 20:09

corecursion


If you do not handle these signals yourself, then, no, the destructors are not called. However, the operating system will reclaim any resources your program used when it terminates.

If you wish to handle signals yourself, then consider checking out the sigaction standard library function.

like image 37
pr1268 Avatar answered Sep 30 '22 21:09

pr1268