Re: Exception-safe constructors

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 10/07/04


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



Relevant Pages

  • Re: Exception handling?
    ... Maybe with FP, exception trapping could now be considered even more important because in classic FP, its not something you think about, you can't if you wish to stack commands which is also the same idea with OOPS based protocol stacking. ... accept "If this function returns TRUE, then the CString & contains the result, but if this ... But I never really understood why a FP approach was not a big consideration for construction: ... With two-step construction you don't need to throw exception in ctor, because the default ctor just puts the object in a safe state and the actual construction is done in a /ad hoc/ construction method, Create...). ...
    (microsoft.public.vc.mfc)
  • Re: Why is the copy ctor called twice here?
    ... > ctor by int: 100 ... > dtor: 100 ... On my compiler, I get what you would expect, namely: ...
    (comp.lang.cpp)
  • Re: Why is the copy ctor called twice here?
    ... > ctor by int: 100 ... > dtor: 100 ... It is possible that the first copy is made when the compiler initializes ... the [constant reference] parameter of 'push_pack' with a temporary ...
    (comp.lang.cpp)
  • Re: is such exception handling approach good?
    ... same time, the construction of m_Whatever is successful, but the construction ... of current instance is failed and exception is thrown -- throw ... if the caller of the constructor MyClass() is smart enough, ... handle a failure outside the ctor like bool initor something. ...
    (microsoft.public.vc.language)
  • Re: CArray
    ... About the copy ctor: I used to think that, if no copy ctor is explicitly ... the compiler provides a default one which does member-wise copy. ... Even if I wonder about the exception system... ... So, if your STL container class throws an exception, this exception is not ...
    (microsoft.public.vc.language)