Exception in a derived class constructor

K

Kannan

Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }
};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!!!!!!!! */
}
};
 
P

peter koch

Kannan skrev:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?
[snip]

This really looks like something for the FAQ:

http://www.parashift.com/c++-faq-lite/

/Peter
 
D

dj

Kannan said:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }
};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!!!!!!!! */
}
};

I believe the c++ primer book specifically mentions that in such cases
the destructors are guaranteed to be called. Anyway, a debug step
through should answer your question without doubt.
 
P

peter koch

dj skrev:
Kannan said:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?
[snip]

I believe the c++ primer book specifically mentions that in such cases
the destructors are guaranteed to be called. Anyway, a debug step
through should answer your question without doubt.
Stepping through the debugger at best shows the behaviour of that
particular compiler - in debug mode. Why not simply look it up?
Probably it is even faster than checking your compilers implementation.
Certainly the look-up gives you far more value for the investment.

/Peter
 
?

=?iso-8859-1?B?UOVobCBNZWxpbg==?=

Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }

};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!!!!!!!! */
}

The short answer is: Yes. The base destructor is guaranteed to be
called. And any objects in the base class and the derived class already
constructed (i.e. aggregate objects). But the tricky part is that the
destructor of the derived object itself is *not* called. The destructor
is only called for fully contructed objects.

So, if you do any non-managed allocation in the constructor you need to
use try/catch and cleanup the memory before exiting the constructor
(since your destructor will *not* be called in this case). Try to use
RAII objects like smart_ptr or auto_ptr to handle memory allocations
and deallocations automatically in case of exceptions.

But the nice thing is that all other objects - base object(s) and
aggregate objects will be destructed as expected.

/ Påhl
 
K

Kannan

OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)
 
D

Diego Martins

Kannan said:
OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)

and what is your compiler ?
 
T

Tomás

Kannan posted:
OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)


Did you compile and run the code I gave you elsethread?


Perhaps your degugger inlined the call to the destructor, and so it
appears that it isn't invoked when you step through -- I've seen that
before.


-Tomás
 
K

Kannan

Disregard my last post.

I was using a MS ver 7.0 compiler and haven't turned on the exception
handler compiler option (/EHa or /EHs). Though the thrown object got
caught in the catch without turning on that option(s). However gcc got
it without giving any compiler options.

So what Påhl Melin said is right. The base class destructor got
called, when exception occurred in derived class constructor. That is
the destructor of all fully constructed objects will get called.

Sorry for the confusion. Thank you for the replies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top