delete, polymorphism, and multiple inheritence

Discussion in 'C++' started by Ian McBride, Jul 19, 2003.

  1. Ian McBride

    Ian McBride Guest

    (was: delete() confusion)

    I have a class with multiple base classes. One of these base classes
    (base1) has its own new/delete operators and nothing else. Another base
    class (base 2) has a virtual destructor. The class with the virtual
    destructor has AddRef() and Release() methods, and Release() ultimately does
    a "delete this". Can that "delete this" statement somehow find the right
    delete method? If it does, it involves a downcast and an upcast to get to
    the right place.

    Beyond this, I am having a fit trying to figure out, I guess, "delete
    visibility" for polymorphic objects. Delete is always static, so it seems
    that the class actually being "deleted" needs to have the class with the
    correct delete method as a base class. But what if you wind up with
    multiple base classes with their own delete operators?

    [I have inherited code with a BUNCH of rocket science assembler level memory
    management put together by a system architect who got his PhD in memory
    strategies. He's gone, and now the new builds are not stable. My first
    choice was to comment out ALL new/delete operators and just use the heap,
    but the system won't stay in real time under load. I think the memory
    management stuff works, but the C++ interface to it -- new and delete -- is
    not safe. He was a memory and architect guru but maybe not a C++ guru. I
    am no kind of guru, but I'm here on a saturday 2 weeks behind a release
    schedule sweating bullets. HELP!!]
     
    Ian McBride, Jul 19, 2003
    #1
    1. Advertising

  2. Ian McBride wrote:
    > (was: delete() confusion)
    >
    > I have a class with multiple base classes. One of these base classes
    > (base1) has its own new/delete operators and nothing else. Another base
    > class (base 2) has a virtual destructor. The class with the virtual
    > destructor has AddRef() and Release() methods, and Release() ultimately does
    > a "delete this". Can that "delete this" statement somehow find the right
    > delete method? If it does, it involves a downcast and an upcast to get to
    > the right place.


    I don't know. It worked ten minutes ago... What does VC++ have to say
    about the listing I posted?

    > Beyond this, I am having a fit trying to figure out, I guess, "delete
    > visibility" for polymorphic objects. Delete is always static, so it seems
    > that the class actually being "deleted" needs to have the class with the
    > correct delete method as a base class. But what if you wind up with
    > multiple base classes with their own delete operators?


    Then you should put a using directive in the derived class for the base
    class operator delete you want to use, or else provide a new operator
    delete in the derived class that does the right thing.

    > [I have inherited code with a BUNCH of rocket science assembler level memory
    > management put together by a system architect who got his PhD in memory
    > strategies. He's gone, and now the new builds are not stable. My first
    > choice was to comment out ALL new/delete operators and just use the heap,
    > but the system won't stay in real time under load. I think the memory
    > management stuff works, but the C++ interface to it -- new and delete -- is
    > not safe. He was a memory and architect guru but maybe not a C++ guru. I
    > am no kind of guru, but I'm here on a saturday 2 weeks behind a release
    > schedule sweating bullets. HELP!!]


    Good luck.
    Buster.
     
    Buster Copley, Jul 19, 2003
    #2
    1. Advertising

  3. On Sat, 19 Jul 2003 13:14:03 -0400, "Ian McBride" <> wrote:

    >(was: delete() confusion)
    >
    >I have a class with multiple base classes. One of these base classes
    >(base1) has its own new/delete operators and nothing else. Another base
    >class (base 2) has a virtual destructor. The class with the virtual
    >destructor has AddRef() and Release() methods, and Release() ultimately does
    >a "delete this". Can that "delete this" statement somehow find the right
    >delete method?


    Yes. In the statement "delete p;", if p is of static type T*, and if T
    has a virtual destructor, and if the object pointed to is of type D derived
    from T, then the D destructor must be invoked first of all, which implies a
    dynamic downcast. From there the destructor action moves up the class
    hierarchy, until finally (all destructors executed) the memory is
    deallocated using "T::eek:perator delete".

    ยง12.5/4 describes the detailed rules for which "operator delete" is called.

    Now this implies that even though "operator delete" is a static function
    it's accessed via some mechanism resembling a virtual method call, i.e.
    in a C++ implementation that uses the vtable mechanism, via the vtable.



    >If it does, it involves a downcast and an upcast to get to
    >the right place.


    The base class parts of an object are not allocated separately from
    the object. The object including all its base class parts is allocated
    in one chunk. If the object is of type T then "T::eek:perator new" is used
    for allocation, and, if it has a virtual destructor, "T::eek:perator delete"
    for deallocation.



    >Beyond this, I am having a fit trying to figure out, I guess, "delete
    >visibility" for polymorphic objects. Delete is always static, so it seems
    >that the class actually being "deleted" needs to have the class with the
    >correct delete method as a base class. But what if you wind up with
    >multiple base classes with their own delete operators?


    I'm not 100% sure whether that is allowed, although I do think so, but
    it does seem like a recipe for disaster. Presumably there's some reason
    for these operators being defined. So instead of inheritance consider
    encapsulation.



    >[I have inherited code with a BUNCH of rocket science assembler level memory
    >management put together by a system architect who got his PhD in memory
    >strategies. He's gone, and now the new builds are not stable. My first
    >choice was to comment out ALL new/delete operators and just use the heap,
    >but the system won't stay in real time under load. I think the memory
    >management stuff works, but the C++ interface to it -- new and delete -- is
    >not safe. He was a memory and architect guru but maybe not a C++ guru. I
    >am no kind of guru, but I'm here on a saturday 2 weeks behind a release
    >schedule sweating bullets. HELP!!]


    As another poster have written, good luck! ;-)
     
    Alf P. Steinbach, Jul 19, 2003
    #3
    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. shruti tiwari via JavaKB.com

    multiple inheritence

    shruti tiwari via JavaKB.com, Mar 30, 2005, in forum: Java
    Replies:
    3
    Views:
    337
    shruti tiwari via JavaKB.com
    Apr 1, 2005
  2. shruti  via JavaKB.com

    multiple inheritence

    shruti via JavaKB.com, Mar 30, 2005, in forum: Java
    Replies:
    11
    Views:
    578
    Thomas G. Marshall
    Apr 3, 2005
  3. Michael C. Starkie

    multiple inheritence question

    Michael C. Starkie, Aug 14, 2003, in forum: C++
    Replies:
    4
    Views:
    356
    Senthilvel Samatharman
    Aug 14, 2003
  4. Derek Basch

    Multiple Inheritence and data attributes

    Derek Basch, May 6, 2005, in forum: Python
    Replies:
    1
    Views:
    236
  5. Krivenok Dmitry
    Replies:
    13
    Views:
    1,474
    Axter
    Jun 1, 2006
Loading...

Share This Page