Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7628853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:40:51+00:00 2026-05-31T05:40:51+00:00

Possible Duplicate: Is destructor called if SIGINT or SIGSTP issued? My code like this:

  • 0

Possible Duplicate:
Is destructor called if SIGINT or SIGSTP issued?

My code like this:

#include <iostream>
#include <signal.h>
#include <cstdlib>

void handler(int) {
    std::cout << "will exit..." << std::endl;
    exit(0);
}

class A {
public:
    A() {std::cout << "constructor" << std::endl;}
    ~A() {std::cout << "destructor" << std::endl;}
};

int main(void) {
    signal(SIGINT, &handler);

    A a;
    for (;;);

    return 0;
}

When I pressed Ctrl-C, it printed:

constructor
^Cwill exit...

There is no “destructor” printed.
So, how can I exit cleanly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:40:52+00:00Added an answer on May 31, 2026 at 5:40 am

    With difficulty. Already, the code you’ve written has undefined
    behavior; you’re not allowed to output to a stream in a signal handler;
    for that matter, you’re not allowed to call exit either. (I’m basing
    my assertions here on the Posix standard. In pure C++, all you’re
    allowed to do is assign to a variable of sig_atomic_t type.)

    In a simple case like your code, you could do something like:

    sig_atomic_t stopFlag = 0;
    
    void
    handler( int )
    {
        stopFlag = 1;
    }
    
    int
    main()
    {
        signal( SIGINT, &handler );
        A a;
        while ( stopFlag == 0 ) {
        }
        std::cout << "will exit..." << std::endl;
        return 0;
    }
    

    Depending on the application, you may be able to do something like this,
    checking the stopFlag at appropriate places. But generally, if you
    try this, there will be race conditions: you check stopFlag before
    starting an interuptable system call, then do the call; the signal
    arrives between the check and the call, you do the call, and it isn’t
    interrupted. (I’ve used this technique, but in an application where the
    only interruptable system call was a socket read with a very short
    timeout.)

    Typically, at least under Posix, you’ll end up having to create a signal
    handling thread; this can then be used to cleanly shut down all of the
    other threads. Basically, you start by setting the signal mask to block
    all signals, then in the signal handling thread, once started, set it to
    accept the signals you’re interested in and call sigwait(). This
    implies, however, that you do all of the usual actions necessary for a
    clean shutdown of the threads: the signal handling thread has to know
    about all other threads, call pthread_cancel on them, etc., and you’re
    compiler has to generate the correct code to handle pthread_cancel, or
    you need to develop some other means of ensuring that all threads are
    correctly notified. (One would hope, today, that all compilers handle
    pthread_cancel correctly. But one never knows; doing so has
    significant runtime cost, and is not usually needed.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why has the destructor been called only once? Given the code below,
Possible Duplicate: class has virtual functions and accessible non-virtual destructor I got this code
Possible Duplicate: difference between destructor and garbage collector Recently i asked this question ,
Possible Duplicate: Will exit() or an exception prevent an end-of-scope destructor from being called?
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
Possible Duplicate: throwing exceptions out of a destructor In C++ we should never throw
Possible Duplicate: Is it worth setting pointers to NULL in a destructor? Do I
Possible Duplicate: Destructors of builtin types (int, char etc..) Template Function: template<typename T> void
Possible Duplicate: When to use virtual destructors? Virtual destructor and undefined behavior i am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.