Destruction order

Discussion in 'C++' started by Christopher, Oct 3, 2011.

  1. Christopher

    Christopher Guest

    The FAQ says, "If you are relying on order dependencies in a virtual
    inheritance hierarchy, you'll need a lot more information than is in
    this FAQ."

    Can someone post that information here?

    I was under the impression that when a base class has a virtual
    destructor, that the destruction order is from most derived to base.

    I've got a tricky smart pointer member that is going to depend on this
    and wanted to verify that it is always guarenteed.

    P.S.
    On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?
    Christopher, Oct 3, 2011
    #1
    1. Advertising

  2. Christopher

    Noah Roberts Guest

    On Oct 3, 9:48 am, Christopher <> wrote:
    > The FAQ says, "If you are relying on order dependencies in a virtual
    > inheritance hierarchy, you'll need a lot more information than is in
    > this FAQ."
    >
    > Can someone post that information here?
    >
    > I was under the impression that when a base class has a virtual
    > destructor, that the destruction order is from most derived to base.


    That is not what the text quoted is referring to. They are referring
    specifically to virtual *inheritance*.

    http://www.cprogramming.com/tutorial/virtual_inheritance.html
    Noah Roberts, Oct 3, 2011
    #2
    1. Advertising

  3. Christopher

    Krice Guest

    On 3 loka, 19:48, Christopher <> wrote:
    > I've got a tricky smart pointer member that is going to depend on
    > this and wanted to verify that it is always guarenteed.


    If you want to do funny unneccessary things in C++ you can
    always test the order of destruction. Add a routine in the
    destructor that prints something on screen.
    Krice, Oct 3, 2011
    #3
  4. Christopher

    Christopher Guest

    On Oct 3, 12:44 pm, Krice <> wrote:
    > On 3 loka, 19:48, Christopher <> wrote:
    >
    > > I've got a tricky smart pointer member that is going to depend on
    > > this and wanted to verify that it is always guarenteed.

    >
    > If you want to do funny unneccessary things in C++ you can
    > always test the order of destruction. Add a routine in the
    > destructor that prints something on screen.


    I don't see why it would be funny or unecessary...
    In fact, I don't see a way around this particular situation at all,
    but then I am not as familiar with the part of boost, that I am
    delving into at this point in time, as I should be.

    I've already debugged through it. I am looking for the _guarentee_ and
    understanding. Just because something debugs one way one day, doesn't
    mean it will be the same after people get a hold of it, derive from
    it, and it is in production. To trust your debugger for a hard fast
    rule would be foolish.
    Christopher, Oct 3, 2011
    #4
  5. Christopher <> writes:

    > The FAQ says, "If you are relying on order dependencies in a virtual
    > inheritance hierarchy, you'll need a lot more information than is in
    > this FAQ."
    >
    > Can someone post that information here?
    >
    > I was under the impression that when a base class has a virtual
    > destructor, that the destruction order is from most derived to base.


    Virtual destructors/members and virtual inheritance are two different
    things. The remark above applies to virtual inheritance (i.e., when a
    class has a public virtual base class).

    Your question is much simpler. Yes, destructors are applied from derived
    to base. If it were the reverse, you would be able to run code in a
    derived dtor on an "incomplete" object (an object that has already
    undergone partial destruction).

    > I've got a tricky smart pointer member that is going to depend on this
    > and wanted to verify that it is always guarenteed.


    Yes.

    > P.S.
    > On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?


    If the FAQ you mention is the one at
    http://www.parashift.com/c -faq-lite/, then the answer to your question
    is... a faq (section [3]).

    -- Alain.
    Alain Ketterlin, Oct 3, 2011
    #5
  6. Christopher

    Krice Guest

    On 3 loka, 21:13, Christopher <> wrote:
    > I don't see why it would be funny or unecessary...


    It's funny when you manage to confuse a fundamental feature of
    a programming language.

    > In fact, I don't see a way around this particular situation at all


    Don't use smart pointers. It looks like they aren't that smart
    after all.
    Krice, Oct 6, 2011
    #6
  7. Christopher

    Werner Guest

    On Oct 3, 7:44 pm, Krice <> wrote:
    > On 3 loka, 19:48, Christopher <> wrote:
    >
    > > I've got a tricky smart pointer member that is going to depend on
    > > this and wanted to verify that it is always guarenteed.

    >
    > If you want to do funny unneccessary things in C++ you can
    > always test the order of destruction. Add a routine in the
    > destructor that prints something on screen.


    Good advice.
    Werner, Oct 6, 2011
    #7
  8. Christopher

    Werner Guest

    On Oct 6, 9:07 am, Krice <> wrote:

    > Don't use smart pointers. It looks like they aren't that smart
    > after all.


    Why give him this advice? You're not making sense. Give
    reasons as to why not to use smart pointers vs. why to use
    them if you want to be of help. True, he confusing
    fundamentals.

    One good reason for using a smart pointer is when
    you forget to initialize it, it has a consistent
    state. Yes, finding uninitialized pointers in a million
    lines of source can be daunting. Finding which one causes
    the bug, well, just rather replace them all.

    Regards,

    Werner
    Werner, Oct 6, 2011
    #8
  9. On 10/3/11 12:48 PM, Christopher wrote:
    > The FAQ says, "If you are relying on order dependencies in a virtual
    > inheritance hierarchy, you'll need a lot more information than is in
    > this FAQ."
    >
    > Can someone post that information here?
    >
    > I was under the impression that when a base class has a virtual
    > destructor, that the destruction order is from most derived to base.
    >
    > I've got a tricky smart pointer member that is going to depend on this
    > and wanted to verify that it is always guarenteed.
    >
    > P.S.
    > On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?
    >


    The standard is very clear on the order that a class hierarchy is
    constructed, (and by the reverse order rule, destructed). The basic rule
    is that when a constructor for a class is entered, it first calls the
    constructor from base classes, in a left to right order, then the
    constructors for its member variables in deceleration order.

    The one hiccup to this order is virtual base classes, as those may be
    mentioned several places in the inheritance tree, but all these
    represent a single sub-object that will only be constructed once. I
    don't know if the standard gives a precise timing of when this object is
    created, but I am fairly sure that it is guaranteed to have been created
    by at least the point where it have been by the basic rule. I suspect
    that virtual base classes will need to be created before any of the
    "normal" base classes, but I don't know it that is promised by the
    standard. Note that if you inherit from a base class that has a virtual
    base class, even if you do not specify that you inherit from that
    virtual base, your constructor is still going to need to construct that
    virtual base, it doesn't get deferred to your base that declared the
    relationship.

    As has been pointed out, this (and what the FAQ is referring to) is
    virtual inheritance, "class foo: virtual pubic bar, public baz {}" not
    just a virtual destructor. A virtual destructor just make sure that the
    right destructor gets called if you delete an object through a pointer
    to a base class (without the virtual destructor, this operation causes
    undefined behavior).
    Richard Damon, Oct 6, 2011
    #9
  10. Christopher

    Christopher Guest

    On Oct 6, 2:07 am, Krice <> wrote:
    > On 3 loka, 21:13, Christopher <> wrote:
    >
    > > I don't see why it would be funny or unecessary...

    >
    > It's funny when you manage to confuse a fundamental feature of
    > a programming language.
    >
    > > In fact, I don't see a way around this particular situation at all

    >
    > Don't use smart pointers. It looks like they aren't that smart
    > after all.


    Not understanding a term "virtual inheritance", due to never using it
    or seeing it before, while being experienced and understanding
    deriving classes and virtual destructors is what I assume you are
    referring to.

    To assert that fact as a reason as to why my design, which you haven't
    seen at all, does something "funny or uneccessary" is a
    philosophically unsound argument.

    Unless of course, my design used or should use "virtual inheritance".
    Neither is the case.
    Christopher, Oct 6, 2011
    #10
  11. Christopher

    Werner Guest

    On Oct 6, 7:16 pm, Christopher <> wrote:


    > Not understanding a term "virtual inheritance", due to never using it
    > or seeing it before, while being experienced and understanding
    > deriving classes and virtual destructors is what I assume you are
    > referring to.


    Well, perhaps I should not give you this link, as you might have
    found it yourself already. I understand that to look for something,
    you need to know about it. In case you hadn't:

    http://en.wikipedia.org/wiki/Virtual_inheritance

    As far as construction/destruction order goes, I can't explain it
    better than the mentioned FAQ's.

    Just search a little bit more ;-) [hint: multiple inheritance]. I'm
    mentioning this as it seems you might have not heard this terminology
    before, and when you look for something and you don't know what you
    are looking for, it's hard...

    Furthermore, I might mention that the order of destruction is always
    the exact opposite to the order of construction. The order of
    destruction is defined in the c++ standard par 12.6.2:10.

    Kind regards,

    Werner
    Werner, Oct 7, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Keil

    Destruction order (e.g. singletons)

    Stephan Keil, Nov 16, 2004, in forum: C++
    Replies:
    0
    Views:
    445
    Stephan Keil
    Nov 16, 2004
  2. Davlet Panech
    Replies:
    4
    Views:
    336
    Davlet Panech
    Nov 10, 2006
  3. Dennis Jones
    Replies:
    2
    Views:
    310
    Dennis Jones
    Jan 5, 2007
  4. BeautifulMind
    Replies:
    7
    Views:
    641
    Ron Natalie
    Feb 8, 2007
  5. Lilith

    Order of destruction

    Lilith, Apr 3, 2008, in forum: C++
    Replies:
    3
    Views:
    467
    James Kanze
    Apr 4, 2008
Loading...

Share This Page