Exceptions in constructor?

Mike Stump mrs@wrs.com
Mon Oct 27 13:26:00 GMT 1997


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



More information about the Gcc mailing list