473,425 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Exception in a derived class constructor

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!!!!!!!!!!!!! */
}
};

May 15 '06 #1
8 2196

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

May 15 '06 #2
dj
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.
May 15 '06 #3

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

May 15 '06 #4
> 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

May 16 '06 #5
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.)

May 16 '06 #6

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 ?

May 16 '06 #7
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
May 16 '06 #8
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.

May 16 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
11
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
11
by: Ivan A. | last post by:
Hi Is there any method to change System. Exception. Message property in derived exception's constructor? I need runtime class' information to initialize this property, but it's readonly. ...
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.