Exception-safe constructors

  • Thread starter =?ISO-8859-1?Q?Fran=E7ois_Duranleau?=
  • Start date
?

=?ISO-8859-1?Q?Fran=E7ois_Duranleau?=

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?
 
A

Alf P. Steinbach

* 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).
 
V

Victor Bazarov

François Duranleau said:
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
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
 
?

=?ISO-8859-1?Q?Fran=E7ois_Duranleau?=

* 9-1?Q?Fran=E7ois_Duranleau?=:

[snip beginning of code]
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!
 
?

=?ISO-8859-1?Q?Fran=E7ois_Duranleau?=

On Wed, 6 Oct 2004, Victor Bazarov wrote:

[snip code]
Add:
try {


Add
} catch (...) { }


... 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_
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top