www.digitalmars.com         C & C++   DMDScript  

D - [bug?] destructors called when exception leaves constructor

reply Mike Swieton <mike swieton.net> writes:
It seems that when an exception is thrown from a constructor, that object's
destructor is still called. There are several problems with this:

- Inconsistant with C++: Only fully constructed objects will have their
  destructors called in C++.
- Dangerous: how does the destructor know what resources to release?

I think this behavior is really asking for trouble.

Mike Swieton
__
Following the light of the sun, we left the Old World.
	- Inscribed on Columbus' caravels
Apr 04 2004
parent reply Ben Hinkle <bhinkle4 juno.com> writes:
On Sun, 04 Apr 2004 21:38:45 -0400, Mike Swieton <mike swieton.net>
wrote:

It seems that when an exception is thrown from a constructor, that object's
destructor is still called. There are several problems with this:

- Inconsistant with C++: Only fully constructed objects will have their
  destructors called in C++.
I believe C++ doesn't initialize the object before calling the constuctor. D initializes before calling the constructor so anything not "constructed" will at least be "initialized".
- Dangerous: how does the destructor know what resources to release?
It just checks if the resource is non-null (or whatever the initialization value is).
I think this behavior is really asking for trouble.

Mike Swieton
__
Following the light of the sun, we left the Old World.
	- Inscribed on Columbus' caravels
Apr 04 2004
parent "Matthew" <matthew stlsoft.org> writes:
I still philosophically feel that this is bad, if it is in fact true.

"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:vpg170dekumvulfe94s40uoq5eenq2gd08 4ax.com...
 On Sun, 04 Apr 2004 21:38:45 -0400, Mike Swieton <mike swieton.net>
 wrote:

It seems that when an exception is thrown from a constructor, that
object's
destructor is still called. There are several problems with this:

- Inconsistant with C++: Only fully constructed objects will have their
  destructors called in C++.
I believe C++ doesn't initialize the object before calling the constuctor. D initializes before calling the constructor so anything not "constructed" will at least be "initialized".
- Dangerous: how does the destructor know what resources to release?
It just checks if the resource is non-null (or whatever the initialization value is).
I think this behavior is really asking for trouble.

Mike Swieton
__
Following the light of the sun, we left the Old World.
 - Inscribed on Columbus' caravels
Apr 10 2004