473,386 Members | 1,803 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,386 software developers and data experts.

Exception-safe constructors

Hi!

I was writing some piece of code and then, after pondering on some
readings in "More Effective C++" by Scott Meyers (Item 10) and the book of
Stroustrup (section 14.4.1 in the hardcover special edition), I tested one
of their solutions to write constructors that avoid memory leaks upon
exceptions. But the leaks were still there!

So I wrote a simple test case and here is the code:

%%%%%

#include <iostream>

using namespace std ;

class chose_binouche
{
public :

chose_binouche()
{
cout << "ctor" << endl ;
}

~chose_binouche()
{
cout << "dtor" << endl ;
}

} ;

class bidon
{
public :

chose_binouche a ;
chose_binouche b ;

bidon()
: a() ,
b()
{
throw "exception" ;
}

~bidon()
{
}

} ;

int
main()
{
bidon b ;
return 0 ;
}

%%%%%

In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the exception
is thrown in the constructor; however, the output of the program was:

ctor
ctor
Aborted (core dumped)

No dtor displayed!

The compiler I am using is g++-3.3.3 and g++-3.4.0 on a Fedora Core 2
Linux station, and I was about to send a bug report but then again maybe
there is something I didn't get right. Unfortunately, I do not have
another compiler at hand to see what it does on other environments.

Can someone else confirm this or prove me wrong?

--
F.D.
Jul 22 '05 #1
4 1548
* 9-1?Q?Fran=E7ois_Duranleau?=:

I was writing some piece of code and then, after pondering on some
readings in "More Effective C++" by Scott Meyers (Item 10) and the book of
Stroustrup (section 14.4.1 in the hardcover special edition), I tested one
of their solutions to write constructors that avoid memory leaks upon
exceptions. But the leaks were still there!

So I wrote a simple test case and here is the code:

%%%%%

#include <iostream>

using namespace std ;

class chose_binouche
{
public :

chose_binouche()
{
cout << "ctor" << endl ;
}

~chose_binouche()
{
cout << "dtor" << endl ;
}

} ;

class bidon
{
public :

chose_binouche a ;
chose_binouche b ;

bidon()
: a() ,
b()
{
throw "exception" ;
}

~bidon()
{
}

} ;

int
main()
{
bidon b ;
return 0 ;
}

%%%%%

In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the exception
is thrown in the constructor;
Nope.

There is no catch for the thrown exception.

In this case the standard specifies a call to std::terminate (or the
installed std::terminate-handler) and unfortunately leaves it unspecified
whether destructors are called or not -- unfortunate because a guarantee
of no destructor-calling in this case would be very nice.
however, the output of the program was:

ctor
ctor
Aborted (core dumped)


Correct (as would be calls of the destructors, unfortunately).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
François Duranleau wrote:
Hi!

I was writing some piece of code and then, after pondering on some
readings in "More Effective C++" by Scott Meyers (Item 10) and the book
of Stroustrup (section 14.4.1 in the hardcover special edition), I
tested one of their solutions to write constructors that avoid memory
leaks upon exceptions. But the leaks were still there!

So I wrote a simple test case and here is the code:

%%%%%

#include <iostream>

using namespace std ;

class chose_binouche
{
public :

chose_binouche()
{
cout << "ctor" << endl ;
}

~chose_binouche()
{
cout << "dtor" << endl ;
}

} ;

class bidon
{
public :

chose_binouche a ;
chose_binouche b ;

bidon()
: a() ,
b()
{
throw "exception" ;
}

~bidon()
{
}

} ;

int
main()
{
Add:
try {
bidon b ;
Add
} catch (...) { }
return 0 ;
}

%%%%%
.... and see if there is any difference...

I tested this on MIPSpro version 7.4 and it gave me

ctor
ctor
dtor
dtor
Caught <exception>


In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the
exception is thrown in the constructor; however, the output of the
program was:

ctor
ctor
Aborted (core dumped)

No dtor displayed!

The compiler I am using is g++-3.3.3 and g++-3.4.0 on a Fedora Core 2
Linux station, and I was about to send a bug report but then again maybe
there is something I didn't get right. Unfortunately, I do not have
another compiler at hand to see what it does on other environments.

Can someone else confirm this or prove me wrong?


See above.

V
Jul 22 '05 #3
On Wed, 6 Oct 2004, Alf P. Steinbach wrote:
* 9-1?Q?Fran=E7ois_Duranleau?=:


[snip beginning of code]

int
main()
{
bidon b ;
return 0 ;
}

%%%%%

In theory (according to the readings mentionned above), the destructors
for the field a and b in class bidon should be called after the exception
is thrown in the constructor;


Nope.

There is no catch for the thrown exception.

In this case the standard specifies a call to std::terminate (or the
installed std::terminate-handler) and unfortunately leaves it unspecified
whether destructors are called or not -- unfortunate because a guarantee
of no destructor-calling in this case would be very nice.


Ah! Either I missed that information or they didn't mention that subtlety
(I do not have a copy of the standard's specification). Anyway, I tried
with an added try-catch block and now it's ok (with g++).

Thanks!

--
F.D.
Jul 22 '05 #4
On Wed, 6 Oct 2004, Victor Bazarov wrote:

[snip code]
int
main()
{


Add:
try {
bidon b ;


Add
} catch (...) { }
return 0 ;
}

%%%%%


... and see if there is any difference...

I tested this on MIPSpro version 7.4 and it gave me

ctor
ctor
dtor
dtor
Caught <exception>


I did the same with g++ and the result is right. Thanks.

__________________________________________________ ________________________
François Duranleau Étudiant Ph.D. Informatique
LIGUM Université de Montréal

"Sacrifices are a necessary factor in creating a new destiny. A small
misfortune becomes the cornerstone of a greater happiness."
- Emperor Dornkirk, in _The Vision of Escaflowne_
Jul 22 '05 #5

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

Similar topics

1
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
5
by: PCC | last post by:
I am using the Exception Managment Application Block on Windows Server 2003 Enterprise and .NET v1.1. If I use the block with an ASP.NET web wervice or in a web application I get the following...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
6
by: Vadivel Kumar | last post by:
I've a problem in handling a custom exception The following is my custom exception class: public class AppException : public Exception { public AppException (string message, Exception...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
7
by: Sek | last post by:
Hi Folks! I was pondering over a code and noticed that exception handlers were present in the private, protected as well as public methods. And, ofcourse, public methods were calling priv/prot...
2
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.