Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does std::terminate() trigger stack unwinding?

I’ve been trying to implement Exception class, and for program termination i’ve decided to use std::terminate(), but i’m not suse whether or not std::terminate() triggers stack unwinding process.

For example, if i compile and run this code:

struct Test {
    Test() {
        std::cout << "Constructed\n";
    }
    ~Test() {
        std::cout << "Destructed\n";
    }
};

int main() {
    Test t;
    std::terminate();
    return 0;
}

it will output this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Constructed
terminate called without an active exception

and it seems that destructor is not being called.

>Solution :

The standard handler for std::terminate() calls directly std::abort.

If you take a look here, you will find out that std::abort() did not call any of the destructors.

Destructors of variables with automatic, thread local (since C++11) and static storage durations are not called. Functions registered with std::atexit() and std::at_quick_exit (since C++11) are also not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading