Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7467

Re: Virtual functions in constructors. Is it possible?

From Leo Equinox Gaspard <ekinox1995@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Virtual functions in constructors. Is it possible?
Date 2011-07-05 13:15 +0200
Organization A noiseless patient Spider
Message-ID <iuurn9$sck$1@dont-email.me> (permalink)
References (7 earlier) <e92f861a-e982-4dec-9f95-bf8286a15187@q1g2000vbj.googlegroups.com> <iut1gm$4dn$1@speranza.aioe.org> <iut4g4$jio$1@dont-email.me> <iutd2n$d9m$1@dont-email.me> <iuuk44$i31$1@dont-email.me>

Show all headers | View raw


Le 05/07/2011 11:09, Alf P. Steinbach /Usenet a écrit :
> * Leo Equinox Gaspard, on 04.07.2011 23:59:
>>
>> Couldn't you [one] use, in ~Initializer :
>>
>> try {
>> if (p)
>> p->Init();
>> } catch(...) {
>> p->InitFailed();
>> throw;
>> }
>
> No.
>
> This could handle exceptions from Init in the sense of reacting to them,
> at the cost of creating a zombie object (unusable object) when such an
> exception occurs.
>
> But for this case it would still yield a memory leak, because this all
> happens after the new-expression has been fully evaluated. When the
> exception occurs in the constructor, the new-expression can clean up
> (and does). When the exception occurs after the new-expression is fully
> evaluated, which is the case with the call of Init, the new-expression
> is already out of the picture, in the past.
>
> And it can't handle the case where the construction of the object that p
> is pointing to, throws an exception.
>
> For then, by the time the ~Initializer runs (at the end of the
> full-expression that instantiates the object), the exception has
> propagated out of the new-expression and, due to the new-expression's
> cleanup, the object that p points to has been destroyed, hence, the call
> 'p->Init()' is now calling a virtual function on a non-existent object,
> which is Undefined Behavior. With g++ the error is caught by the
> runtime, and you get a crash. With MSVC it's not caught, and you get
> more subtle errors.
>

Oh. I thought the exception was in Init().
The run of the program I figured was :

=> new Derived()
  => Instantiation of an Initializer
  => Only do it, we don't want *any* exception in this constructor
=> End of new
=> ~Initializer()
  => Call to Init(), catching exceptions
   => Init() does the whole construction stuff, additionnally calling
      Base's Init()
  => If any exception was caught, then clear the work done by Init()
     using InitFailed()
   => InitFailed() should be exception-less and make the object ready to
      be destructed.
  => Then rethrow the exception
=> End of ~Initializer
=> If any exception is thrown, then the object is not valid, and hence 
should not be used, but immediately destructed. That's the default 
behavior of the exception, which will make any scoped items to be deleted.
Of course, using a raw pointer will lead to memory leaks, but that's 
always as it with them plus exceptions.

Where am I mistaking ?

Cheers,
Leo

>
>> And implement an InitFailed() virtual method that acts as the
>> countrary (not
>> sure of the word to use there) of Init ?
>>
>> So the code should have no more memory leaks.
>
> Sorry, no, see above.
>
>
> Cheers & hth.,
>
> - Alf
>

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 00:19 +0200
  Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 00:37 +0200
    Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 01:19 +0200
      Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 08:50 +0200
        Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 10:50 +0200
          Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 11:17 +0200
            Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 11:25 +0200
              Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-02 11:31 +0200
              Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-02 12:00 +0200
              Re: Virtual functions in constructors. Is it possible? Werner <werasm@gmail.com> - 2011-07-04 01:10 -0700
                Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-04 20:41 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-04 21:35 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-04 23:59 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 11:09 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-05 13:15 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 14:51 +0200
                Re: Virtual functions in constructors. Is it possible? Leo Equinox Gaspard <ekinox1995@gmail.com> - 2011-07-05 19:55 +0200
                Re: Virtual functions in constructors. Is it possible? Anonymous <anonymous@no.net> - 2011-07-05 20:46 +0200
                Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-05 21:23 +0200
                Re: Virtual functions in constructors. Is it possible? Werner <werasm@gmail.com> - 2011-07-06 00:14 -0700
  Re: Virtual functions in constructors. Is it possible? Virchanza <virtual@lavabit.com> - 2011-07-05 14:58 -0700
    Re: Virtual functions in constructors. Is it possible? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-06 01:28 +0200

csiph-web