Click here to Skip to main content
13,086,120 members (82,358 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 4 Dec 2009

When a C++ destructor did not run – Part 1

, 4 Dec 2009
Rate this:
Please Sign up or sign in to vote.
Consider this piece of C++ code. 1: using namespace std; 2:  3: class C 4: { 5: public: 6: C() 7: { 8: cout << "Constructed"; 9: } 10: ~C() 11: { 12:
 1: using namespace std;
 3: class C
 4: {
 5: public:
 6:     C()
 7:     {
 8:         cout << "Constructed";
 9:     }
10:     ~C()
11:     {
12:         cout << "Destructed";
13:     }
14: };
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.


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


About the Author

S. Senthil Kumar
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.

You may also be interested in...

Comments and Discussions

GeneralVery Interesting Pin
Rama Krishna Vavilala4-Dec-09 15:35
memberRama Krishna Vavilala4-Dec-09 15:35 
GeneralRe: Very Interesting Pin
S. Senthil Kumar5-Dec-09 17:07
memberS. 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 4 Dec 2009
Article Copyright 2009 by S. Senthil Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid