Click here to Skip to main content
15,880,956 members
Articles / Programming Languages / C++

When a C++ Destructor Did Not Run – Part 1

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Dec 2009CPOL1 min read 12K   2   2
When a C++ destructor did not run
C++
 1: using namespace std;
 2:
 3: class C
 4: {
 5: public:
 6:     C()
 7:     {
 8:         cout << "Constructed";
 9:     }
10:     ~C()
11:     {
12:         cout << "Destructed";
13:     }
14: };
15:
16: void SomeFunc()
17: {
18:     C c;
19:     throw std::exception("Gone");
20: }

If you know any C++ at all, you’ll know that when SomeFunc returns, both “Constructed” and “Destructed” will be printed to the console. That is because RAII in C++ guarantees that the destructor of an object created on the stack will always run when control leaves the scope, no matter what.

You put all this code is in a static library, say PureCPP.lib, and you compile it with the /EHs option, because you want to use C++ exceptions.

You then write a native application to consume this library, statically link to it, and everything works great.

One day, you wake up and realize you’ll have to try out this .NET stuff that everyone is talking about. You discover that there’s this language called C++/CLI that’s great for interfacing with native code. So you fire up VS, create a CLR console application that calls SomeFunc, and link PureCPP.lib against it.

Just when you’re wondering how easy things turned out to be, you notice something strange. There’s something missing in the console output. When you figure out what’s missing, your jaw hits the ground. Mine did too, when I realized that it was the “Destructed” part that was missing. Which means the impossible just happened - the destructor for class C did not run.

What followed was a long and exciting journey into the world of SEH (Structured Exception Handling), exception codes and exception propagation and handling by the CLR versus C++. All that in the next part – stay tuned.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

Comments and Discussions

 
GeneralVery Interesting Pin
Rama Krishna Vavilala4-Dec-09 15:35
Rama Krishna Vavilala4-Dec-09 15:35 
GeneralRe: Very Interesting Pin
S. Senthil Kumar5-Dec-09 17:07
S. Senthil Kumar5-Dec-09 17:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.