Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Exception in a derived class constructor

Reply
Thread Tools

Exception in a derived class constructor

 
 
Kannan
Guest
Posts: n/a
 
      05-15-2006
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!!!!!!!!!!!!! */
}
};

 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      05-15-2006

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

 
Reply With Quote
 
 
 
 
dj
Guest
Posts: n/a
 
      05-15-2006
Kannan wrote:
> 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.
 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      05-15-2006

dj skrev:

> Kannan wrote:
> > 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

 
Reply With Quote
 
=?iso-8859-1?B?UOVobCBNZWxpbg==?=
Guest
Posts: n/a
 
      05-16-2006
> 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

 
Reply With Quote
 
Kannan
Guest
Posts: n/a
 
      05-16-2006
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.)

 
Reply With Quote
 
Diego Martins
Guest
Posts: n/a
 
      05-16-2006

Kannan wrote:
> 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 ?

 
Reply With Quote
 
Tomás
Guest
Posts: n/a
 
      05-16-2006
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
 
Reply With Quote
 
Kannan
Guest
Posts: n/a
 
      05-16-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57