Looking for an answer for C# and C++. (in C#, replace 'destructor' with 'finalizer')
Preamble: Herb Sutter has a great article on the subject:
http://herbsutter.wordpress.com/2008/07/25/constructor-exceptions-in-c-c-and-java/
C++ : Yes and No
While an object destructor won't be called if its constructor throws (the object "never existed"), the destructors of its internal objects could be called.
As a summary, every internal parts of the object (i.e. member objects) will have their destructors called in the reverse order of their construction. Every thing built inside the constructor won't have its destructor called unless RAII is used in some way.
For example:
struct Class
{
Class() ;
~Class() ;
Thing * m_pThing ;
Object m_aObject ;
Gizmo * m_pGizmo ;
Data m_aData ;
}
Class::Class()
{
this->m_pThing = new Thing() ;
this->m_pGizmo = new Gizmo() ;
}
The order of creation will be:
- m_aObject will have its constructor called.
- m_aData will have its constructor called.
- Class constructor is called
- Inside Class constructor, m_pThing will have its new and then constructor called.
- Inside Class constructor, m_pGizmo will have its new and then constructor called.
Let's say we are using the following code:
Class pClass = new Class() ;
Some possible cases:
Should m_aData throw at construction, m_aObject will have its destructor called. Then, the memory allocated by "new Class" is deallocated.
Should m_pThing throw at new Thing (out of memory), m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.
Should m_pThing throw at construction, the memory allocated by "new Thing" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.
Should m_pGizmo throw at construction, the memory allocated by "new Gizmo" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated. Note that m_pThing leaked
If you want to offer the Basic Exception Guarantee, you must not leak, even in the constructor. Thus, you'll have to write this this way (using STL, or even Boost):
struct Class
{
Class() ;
~Class() ;
std::auto_ptr<Thing> m_pThing ;
Object m_aObject ;
std::auto_ptr<Gizmo> m_pGizmo ;
Data m_aData ;
}
Class::Class()
: m_pThing(new Thing())
, m_pGizmo(new Gizmo())
{
}
Or even:
Class::Class()
{
this->m_pThing.reset(new Thing()) ;
this->m_pGizmo.reset(new Gizmo()) ;
}
if you want/need to create those objects inside the constructor.
This way, no matter where the constructor throws, nothing will be leaked.
It does for C# (see code below) but not for C++.
using System;
class Test
{
Test()
{
throw new Exception();
}
~Test()
{
Console.WriteLine("Finalized");
}
static void Main()
{
try
{
new Test();
}
catch {}
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
This prints "Finalized"
Preamble: Herb Sutter has a great article on the subject:
http://herbsutter.wordpress.com/2008/07/25/constructor-exceptions-in-c-c-and-java/
C++ : Yes and No
While an object destructor won't be called if its constructor throws (the object "never existed"), the destructors of its internal objects could be called.
As a summary, every internal parts of the object (i.e. member objects) will have their destructors called in the reverse order of their construction. Every thing built inside the constructor won't have its destructor called unless RAII is used in some way.
For example:
struct Class
{
Class() ;
~Class() ;
Thing * m_pThing ;
Object m_aObject ;
Gizmo * m_pGizmo ;
Data m_aData ;
}
Class::Class()
{
this->m_pThing = new Thing() ;
this->m_pGizmo = new Gizmo() ;
}
The order of creation will be:
- m_aObject will have its constructor called.
- m_aData will have its constructor called.
- Class constructor is called
- Inside Class constructor, m_pThing will have its new and then constructor called.
- Inside Class constructor, m_pGizmo will have its new and then constructor called.
Let's say we are using the following code:
Class pClass = new Class() ;
Some possible cases:
Should m_aData throw at construction, m_aObject will have its destructor called. Then, the memory allocated by "new Class" is deallocated.
Should m_pThing throw at new Thing (out of memory), m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.
Should m_pThing throw at construction, the memory allocated by "new Thing" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated.
Should m_pGizmo throw at construction, the memory allocated by "new Gizmo" will be deallocated. Then m_aData, and then m_aObject will have their destructors called. Then, the memory allocated by new Class is deallocated. Note that m_pThing leaked
If you want to offer the Basic Exception Guarantee, you must not leak, even in the constructor. Thus, you'll have to write this this way (using STL, or even Boost):
struct Class
{
Class() ;
~Class() ;
std::auto_ptr<Thing> m_pThing ;
Object m_aObject ;
std::auto_ptr<Gizmo> m_pGizmo ;
Data m_aData ;
}
Class::Class()
: m_pThing(new Thing())
, m_pGizmo(new Gizmo())
{
}
Or even:
Class::Class()
{
this->m_pThing.reset(new Thing()) ;
this->m_pGizmo.reset(new Gizmo()) ;
}
if you want/need to create those objects inside the constructor.
This way, no matter where the constructor throws, nothing will be leaked.
The destructor of the class still being constructed is not called, because the object was never fully constructed.
However, the destructor of its base class (if any) IS called, because the object was constructed as far as being a base class object.
Moreover, any member variables will have their destructors called too (as others have noted).
NB: this applies to C++
In C++, the answer is no - object's destructor is not called.
However, the destructors of any member data on the object will be called, unless the exception was thrown while constructing one of them.
Member data in C++ is initialized (i.e. constructed) in the same order as it is declared, so when the constructor throws, all member data that has been initialized - either explicitly in the Member Initialization List (MIL) or otherwise - will be torn down again in reverse order.
T*
member initialized with new T
, then you actually have two objects: the new'ed T and the T*
itself. Like an int
, a T*
has no destructor. Hence, when the T*
member is deleted, the T pointed to is not. –
Chelseychelsie If the constructor doesn't finish executing, the object doesn't exist, so there's nothing to destruct. This is in C++, I have no idea about C#.
For C++ this is addressed in a previous question: Will the below code cause memory leak in c++
Since in C++ when an exception is thrown in a constructor the destructor does not get called, but dtors for the object's members (that have been constructed) do get called, this is a primary reason to use smart pointer objects over raw pointers - they are a good way to prevent memory leaks in a situation like this.
C++ -
Nope. Destructor is not called for partially constructed objects. A Caveat: The destructor will be called for its member objects which are completely constructed. (Includes automatic objects, and native types)
BTW - What you're really looking for is called "Stack Unwinding"
Don't do things that cause exceptions in the constructor.
Call an Initialize() after the constructor that can throw exceptions.
© 2022 - 2024 — McMap. All rights reserved.