Re: Exception-safe constructors
From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 10/07/04
- Next message: David Hilsee: "Re: RefCounts"
- Previous message: Default User: "Re: need help with a While i'm a beginner"
- In reply to: François Duranleau: "Exception-safe constructors"
- Next in thread: François Duranleau: "Re: Exception-safe constructors"
- Reply: François Duranleau: "Re: Exception-safe constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 06 Oct 2004 18:11:38 -0400
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
- Next message: David Hilsee: "Re: RefCounts"
- Previous message: Default User: "Re: need help with a While i'm a beginner"
- In reply to: François Duranleau: "Exception-safe constructors"
- Next in thread: François Duranleau: "Re: Exception-safe constructors"
- Reply: François Duranleau: "Re: Exception-safe constructors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|