Is destructor called if SIGINT or SIGSTP issued?

前端 未结 3 1954
情歌与酒
情歌与酒 2020-12-01 00:08

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 t

3条回答
  •  感动是毒
    2020-12-01 00:46

    Let's try it:

    #include 
    #include 
    
    class Foo {
    public:
      Foo() {};
      ~Foo() { printf("Yay!\n"); }
    } bar;
    
    int main(int argc, char **argv) {
      sleep(5);
    }
    

    And then:

    $ g++ -o test ./test.cc 
    $ ./test 
    ^C
    $ ./test 
    Yay!
    

    So I'm afraid not, you'll have to catch it.

    As for SIGSTOP, it cannot be caught, and pauses the process until a SIGCONT is sent.

提交回复
热议问题