The next code doesn’t call object’s destructor when exception is thrown if compiled with GCC or MS Visual C++:

#include <iostream>
#include <stdexcept>
struct A {
    A () { std::cout << "A" << std::endl; }
    ~A () { std::cout << "~A" << std::endl; }
void myfunc () {
    A temp;
    throw std::runtime_error("moo");
int main () {

This is caused by lack of matching catch() handler –if you add it destructor will be called properly.
Looking at the C++ standard, section 15.3 [except.handle] point 9, we see:
If no matching handler is found in a program, the function terminate()
(_except.terminate_) is called. Whether or not the stack is unwound
before calling terminate() is implementation-defined.
Both gcc and Visual Studio behave the same in this context, and do not perform full stack unwinding in this case.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s