Re: Exception unwinding base destructor called - why?

From: Douglas Peterson (Tergiver_at_nospam.msn.com)
Date: 06/24/04


Date: Thu, 24 Jun 2004 11:17:06 -0400

Thanks to Andre, John, and David for taking the time to reply.

After your explanations and giving it more thought, I accept that a fully
constucted object needs to be deconstructed. Makes perfect sense when you
look at it outside of the problem your having :)

Here's what the issue was and how I'm going to change it:

The objects in my system are all contained in lists. There is a root item
with a list of objects, those objects contain a list of objects and so on,
and so on.

I wanted two properties for objects that are contained in this system:

1) Objects could be created externally so that they can be further derived
by the user. That is to say you don't call Container.CreateObject() to
create one, you can 'new Object'.
2) Objects would automagically be added and removed to/from the containers
they belong to.

To accomplish #2, the constructors of the object's deepest base class were
adding and removing themselves to their owner's lists. When an exception
occurs during object construction, one of two possibilities occurs depending
on what point the exception is thrown:

1) The object has been added to the container's list, but doesn't (can't)
remove itself when its not getting fully constructed. This leads to an
attempt by the container to delete an already deleted object.
2) The object isn't yet added, but its destructor tries to unlist itself.

Number 1 can be solved by placing the 'insert myself into my owner's
container' line of code dead last in the constructor. That way if an
exception occurs during its construction, its not added. If the exception
occurs in a derived class, we get number 2 because the base is fully
constructed and will be destructed.
Number 2 is benign, however, my container list code asserts because in most
cases the programmer wants to know that his code is trying to remove
something that isn't there (a potential bug).

So I'm left with a choice:

1) Remove the automagic and require all objects to be inserted after they
are constructed.
2) Remove the assertion from my list class to allow for benign attempts at
removal.

I'll elicit some respones before making a descision.



Relevant Pages

  • Re: Exception unwinding base destructor called - why?
    ... >> adding and removing themselves to their owner's lists. ... >> attempt by the container to delete an already deleted object. ... >> exception occurs during its construction, ... > Here's how I see it, the deepest base class ctor adds to the list, the ...
    (comp.lang.cpp)
  • Re: Exception unwinding base destructor called - why?
    ... > adding and removing themselves to their owner's lists. ... > on what point the exception is thrown: ... > exception occurs during its construction, ... Here's how I see it, the deepest base class ctor adds to the list, the ...
    (comp.lang.cpp)
  • Re: Why keep identity-based equality comparison?
    ... The cgi module turns the request string into a dictionary-like container of objects with values of different types. ... It revolved around lists of things to play, and the "things" in question could be any "playable" object - video or audio files, track on a CD, or a DVD, or even a playlist. ... probably raise an exception if you tried, but I can't decide whether that's a good or a bad thing... ...
    (comp.lang.python)
  • Re: Why keep identity-based equality comparison?
    ... > heterogeneous containers where you've used the "in" operator. ... heterogenous container, you have to deal with this. ... >> homogenous lists of heterogenous containers. ... > probably raise an exception if you tried, ...
    (comp.lang.python)
  • Re: How come Ada isnt more popular?
    ... are praising is, because what about trees of strings, trees of lists etc. ... A language with a Hinldey-Milner type system ... container varies, the element does not. ...
    (comp.lang.ada)

Loading