Home > other >  Does std::terminate() trigger stack unwinding?
Does std::terminate() trigger stack unwinding?

Time:04-05

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:

Constructed
terminate called without an active exception

and it seems that destructor is not being called.

CodePudding user response:

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.

  • Related