public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Exceptions in constructor?
@ 1997-10-25  9:35 chip
  1997-10-27  6:21 ` Paul Koning
  0 siblings, 1 reply; 11+ messages in thread
From: chip @ 1997-10-25  9:35 UTC (permalink / raw)
  To: egcs

I'm 99.44% pleased with the exception-based code I can now write and
test under Linux on x86.  The remaining .56% would be fulfilled if
throwing an exception in a constructor automatically called the
appropriate operator delete () if there had been a call to operator
new ().

Has anyone made progress on that?  If not, can anyone give me a hint
where I might look to put it in?  Thanks muchly in advance....
-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-25  9:35 Exceptions in constructor? chip
@ 1997-10-27  6:21 ` Paul Koning
  1997-10-27  6:38   ` chip
  1997-10-27 12:57   ` Alexandre Oliva
  0 siblings, 2 replies; 11+ messages in thread
From: Paul Koning @ 1997-10-27  6:21 UTC (permalink / raw)
  To: chip; +Cc: egcs

>>>>> "chip" == chip  <chip@atlantic.net> writes:

 chip> I'm 99.44% pleased with the exception-based code I can now
 chip> write and test under Linux on x86.  The remaining .56% would be
 chip> fulfilled if throwing an exception in a constructor
 chip> automatically called the appropriate operator delete () if
 chip> there had been a call to operator new ().

Does the C++ standard say to do that?  It would surprise me.  If it
doesn't, it would be a major problem if egcs did it anyway.

	paul

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27  6:21 ` Paul Koning
@ 1997-10-27  6:38   ` chip
  1997-10-27 11:52     ` ernst.molitor
  1997-10-27 12:57   ` Alexandre Oliva
  1 sibling, 1 reply; 11+ messages in thread
From: chip @ 1997-10-27  6:38 UTC (permalink / raw)
  To: Paul Koning; +Cc: chip, egcs

According to Paul Koning:
> >>>>> "chip" == chip  <chip@atlantic.net> writes:
>  chip> I'm 99.44% pleased with the exception-based code I can now
>  chip> write and test under Linux on x86.  The remaining .56% would be
>  chip> fulfilled if throwing an exception in a constructor
>  chip> automatically called the appropriate operator delete () if
>  chip> there had been a call to operator new ().
> 
> Does the C++ standard say to do that?  It would surprise me.

Of course!  Without it, exceptions thrown in (or through) constructors
would cause irreparable memory leaks.
-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27  6:38   ` chip
@ 1997-10-27 11:52     ` ernst.molitor
  1997-10-27 12:10       ` chip
                         ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: ernst.molitor @ 1997-10-27 11:52 UTC (permalink / raw)
  To: chip; +Cc: pkoning, chip, egcs

Dear discussants, 

:According to Paul Koning:
:> >>>>> "chip" == chip  <chip@atlantic.net> writes:
:>  chip> I'm 99.44% pleased with the exception-based code I can now
:>  chip> write and test under Linux on x86.  The remaining .56% would be
:>  chip> fulfilled if throwing an exception in a constructor
:>  chip> automatically called the appropriate operator delete () if
:>  chip> there had been a call to operator new ().
:>
:> Does the C++ standard say to do that?  It would surprise me.
:
:Of course!  Without it, exceptions thrown in (or through) constructors
:would cause irreparable memory leaks.

I don't have the standard at hands, but B. Stroustrup, in his book
"C++ Programming Language" (3rd ed. paperback), states that a
destructor will be called only for objects that are "completely
constructed" (p. 367).  Depending on the source code in question, this
may mean that an exception thrown in a constructor will _not_ cause a
call of the destructor, where the call to delete would be expected (an
example is given in the book, with a note that memory leaks may be
caused by this...).

Best regards,

Ernst

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27 11:52     ` ernst.molitor
@ 1997-10-27 12:10       ` chip
  1997-10-27 13:36       ` Alexandre Oliva
  1997-10-27 15:54       ` John A. Tamplin
  2 siblings, 0 replies; 11+ messages in thread
From: chip @ 1997-10-27 12:10 UTC (permalink / raw)
  To: ernst.molitor; +Cc: chip, pkoning, egcs

According to ernst.molitor@uni-bonn.de:
> :According to Paul Koning:
> :> >>>>> "chip" == chip  <chip@atlantic.net> writes:
> :>  chip> I'm 99.44% pleased with the exception-based code I can now
> :>  chip> write and test under Linux on x86.  The remaining .56% would be
> :>  chip> fulfilled if throwing an exception in a constructor
> :>  chip> automatically called the appropriate operator delete () if
> :>  chip> there had been a call to operator new ().
> :>
> :> Does the C++ standard say to do that?  It would surprise me.
> :
> :Of course!  Without it, exceptions thrown in (or through) constructors
> :would cause irreparable memory leaks.
> 
> I don't have the standard at hands, but B. Stroustrup, in his book
> "C++ Programming Language" (3rd ed. paperback), states that a
> destructor will be called only for objects that are "completely
> constructed" (p. 367).  Depending on the source code in question, this
> may mean that an exception thrown in a constructor will _not_ cause a
> call of the destructor, where the call to delete would be expected...

A delete operator can be called without calling the destructor.  That
is, in fact, what the draft ANSI standard requires, when a constructor
of an object created with 'new Class' throws an exception.
-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27  6:21 ` Paul Koning
  1997-10-27  6:38   ` chip
@ 1997-10-27 12:57   ` Alexandre Oliva
  1 sibling, 0 replies; 11+ messages in thread
From: Alexandre Oliva @ 1997-10-27 12:57 UTC (permalink / raw)
  To: Paul Koning; +Cc: chip, egcs

Paul Koning writes:

>>>>>> "chip" == chip  <chip@atlantic.net> writes:
chip> throwing an exception in a constructor
chip> automatically called the appropriate operator delete () if
chip> there had been a call to operator new ().

> Does the C++ standard say to do that?

It does, in [expr.new]/17-20

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27 11:52     ` ernst.molitor
  1997-10-27 12:10       ` chip
@ 1997-10-27 13:36       ` Alexandre Oliva
  1997-10-27 15:54       ` John A. Tamplin
  2 siblings, 0 replies; 11+ messages in thread
From: Alexandre Oliva @ 1997-10-27 13:36 UTC (permalink / raw)
  To: ernst.molitor; +Cc: chip, pkoning, egcs

ernst molitor writes:

> destructor will be called only for objects that are "completely
> constructed" (p. 367)

that's right, but this discussion has nothing to do with calling the
destructor.  The point is whether or not operator delete should be
called to free the memory allocated by operator new, if the
constructor called by the new expression throws an exception.  The
Dec'96 C++ CD says it should.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
  1997-10-27 11:52     ` ernst.molitor
  1997-10-27 12:10       ` chip
  1997-10-27 13:36       ` Alexandre Oliva
@ 1997-10-27 15:54       ` John A. Tamplin
  1997-10-27 17:36         ` [ECGS] " chip
  2 siblings, 1 reply; 11+ messages in thread
From: John A. Tamplin @ 1997-10-27 15:54 UTC (permalink / raw)
  To: ernst.molitor; +Cc: egcs

On Mon, 27 Oct 1997 ernst.molitor@uni-bonn.de wrote:

> I don't have the standard at hands, but B. Stroustrup, in his book
> "C++ Programming Language" (3rd ed. paperback), states that a
> destructor will be called only for objects that are "completely
> constructed" (p. 367).  Depending on the source code in question, this
> may mean that an exception thrown in a constructor will _not_ cause a
> call of the destructor, where the call to delete would be expected (an
> example is given in the book, with a note that memory leaks may be
> caused by this...).

You are referring to the case of exceptions occuring within a constructor,
being thrown past the block where the object being constructed lives, and
the destructor not being called since the object is not completely 
constructed.  I believe the original post was referring to how the compiler
builds a default constructor, in that it should catch exceptions after
memory is allocated and free that memory before passing any exceptions to
the caller.  If an exception occurs in operator new, it needs to catch that
exception and clean up after itself before passing it up the chain.

John Tamplin					Traveller Information Services
jat@Traveller.COM				2104 West Ferry Way
205/883-4233x7007				Huntsville, AL 35801


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [ECGS] Exceptions in constructor?
  1997-10-27 15:54       ` John A. Tamplin
@ 1997-10-27 17:36         ` chip
  0 siblings, 0 replies; 11+ messages in thread
From: chip @ 1997-10-27 17:36 UTC (permalink / raw)
  To: John A. Tamplin; +Cc: ernst.molitor, egcs

According to John A. Tamplin:

> I believe the original post was referring to how the compiler builds
> a default constructor, in that it should catch exceptions after
> memory is allocated and free that memory before passing any
> exceptions to the caller.  If an exception occurs in operator new,
> it needs to catch that exception and clean up after itself before
> passing it up the chain.

No, the original post (mine) was talking about exceptions in the
constructor, and how they require calls to operator delete () if the
object being constructed is not auto.

Exceptions thrown in operator new () are relatively simple.
-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
@ 1997-10-27 18:40 Mike Stump
  0 siblings, 0 replies; 11+ messages in thread
From: Mike Stump @ 1997-10-27 18:40 UTC (permalink / raw)
  To: pkoning; +Cc: egcs

> Date: Mon, 27 Oct 1997 09:19:43 -0500
> From: Paul Koning <pkoning@xedia.com>
> To: chip@pobox.com
> Cc: egcs@cygnus.com

> >>>>> "chip" == chip  <chip@atlantic.net> writes:

>  chip> I'm 99.44% pleased with the exception-based code I can now
>  chip> write and test under Linux on x86.  The remaining .56% would be
>  chip> fulfilled if throwing an exception in a constructor
>  chip> automatically called the appropriate operator delete () if
>  chip> there had been a call to operator new ().

> Does the C++ standard say to do that?  It would surprise me.

Be surprised then!

2 An object that is partially constructed will have destructors executed
  only  for its fully constructed sub-objects.  Should a constructor for
  an element  of  an  automatic  array  throw  an  exception,  only  the
  constructed  elements  of that array will be destroyed.  If the object
  or array was allocated in a  new-expression and  the    new-expression
  does not contain a  new-placement, the deallocation function (3.7.3.2,
  12.5) is called to free  the  storage  occupied  by  the  object;  the
  deallocation  function is chosen as specified in 5.3.4.  If the object
  or array was allocated in a  new-expression and  the    new-expression
  contains  a    new-placement,  the  storage  occupied by the object is
  deallocated only if an appropriate placement operator delete is found,
  as specified in 5.3.4.

You don't have to speculate what the standard says, just read it.  It
is online at http://www.cygnus.com/misc/wp/dec96pub/ .  Speculating
that the standard doesn't say things that is says just confuses others
that don't know what it says.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Exceptions in constructor?
@ 1997-10-27 13:26 Mike Stump
  0 siblings, 0 replies; 11+ messages in thread
From: Mike Stump @ 1997-10-27 13:26 UTC (permalink / raw)
  To: chip, egcs

> From: chip@atlantic.net
> To: egcs@cygnus.com
> Date: Sat, 25 Oct 1997 12:35:25 -0400 (EDT)

> I'm 99.44% pleased with the exception-based code I can now write and
> test under Linux on x86.

Glad to hear it.

> The remaining .56% would be fulfilled if throwing an exception in a
> constructor automatically called the appropriate operator delete ()
> if there had been a call to operator new ().

> Has anyone made progress on that?  If not, can anyone give me a hint
> where I might look to put it in?  Thanks muchly in advance....

While generating code for a constructor call, wrap call with EH
region, if we get a throw after allocation but before construction
completes, call delete on the pointer to the object.

You'll need to pay special care to classes with virtual bases, and
region ordering (function try block on constructor v constructor with
throw() v try block just inside {} of the constructor), and possibly
inlining depending upon where you put the new region.  It would be
nice to elide the region if possible.

Also, placement delete should be called if placement new throws.

I have a feeling the region must go in the caller of the constructor,
probably in build_new and/or friends.  You can use the style of
build_array_eh_cleanup, which does almost the same thing, but it is
responsible for cleaning up partially built arrays.

You'll have to be careful which obstack the handler is placed on as
well.

Hope this is enough to get you started.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1997-10-27 18:40 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-10-25  9:35 Exceptions in constructor? chip
1997-10-27  6:21 ` Paul Koning
1997-10-27  6:38   ` chip
1997-10-27 11:52     ` ernst.molitor
1997-10-27 12:10       ` chip
1997-10-27 13:36       ` Alexandre Oliva
1997-10-27 15:54       ` John A. Tamplin
1997-10-27 17:36         ` [ECGS] " chip
1997-10-27 12:57   ` Alexandre Oliva
1997-10-27 13:26 Mike Stump
1997-10-27 18:40 Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).