Deleting derived classes

This is a discussion on Deleting derived classes within the C++ Programming forums, part of the General Programming Boards category; Hi, i am writing a client for some protocol which has several messages defined. Ive been passing these messages around ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    Deleting derived classes

    Hi, i am writing a client for some protocol which has several messages defined. Ive been passing these messages around in a list of type <Message*>, whilst each individual message is derived from Message, let me give a quick example of this, with a message called GRMFYI

    Code:
    int main (int argc, char** argv)
    {
            DatReader* dat = NULL;
            GameState* gs = NULL;
    
            uint8_t* inpacket;
            int packet_size = loadpacket (&inpacket);
    
            uint8_t* tmp = new uint8_t[packet_size];
            memcpy (tmp, inpacket, packet_size);
    
            NetworkMessage* inmsg = new NetworkMessage (packet_size, tmp);
            inmsg->setPos (4);
    
            Message* m = new GRMFYI (inmsg, gs, dat);
    
            delete m;
            delete inpacket;
            delete inmsg;
            return 0;
    }
    this is stripped down to absolute basics, but basically when i run valgrind GRMFYI wont be freed, i assume because Message and GRMFYI are different. I can only guess that the wrong deconstructor is being called, as frees are done based on structures in memory (i think). If instead of
    Code:
            Message* m = new GRMFYI (inmsg, gs, dat);
    i use
    Code:
            GRMFYI* m = new GRMFYI (inmsg, gs, dat);
    valgrind gives me a happy pat on the back. My question is is there a way to delete a derived class if you only have a pointer to the base class and are unaware of how the class was derived.

    The two obvious solutions are to have an virtual killme () function which does delete this or to add a deleteMessage function to the message factory (i already have one producing the derived classes).
    Thankyou

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    You simply need to make sure that the destructor (note spelling) of 'Message' itself is virtual (which means it's derived classes will also have a virtual destructor). This is standard practice for classes that need to be deleted through a pointer to their base type. It should make the code work correctly, and prevent complaints from valgrind.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    it is virtual, and i think i know what your talking about. The problem is im deleteing the base class, not the derived class.
    I know that when you delete a derived class the constructor of the base class is also called.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    For anyones interest i took the latter approach and gave factories the ability to delete messages by looking up their ids and then choosing an appropriate type to cast to before deleting them.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Quote Originally Posted by *DEAD* View Post
    it is virtual, and i think i know what your talking about. The problem is im deleteing the base class, not the derived class.
    I know that when you delete a derived class the constructor of the base class is also called.
    No, if the destructor is virtual, then you can delete the object through a pointer to the base class. Basic polymorphism.
    If it doesn't work, then something else is wrong.

    Quote Originally Posted by *DEAD* View Post
    For anyones interest i took the latter approach and gave factories the ability to delete messages by looking up their ids and then choosing an appropriate type to cast to before deleting them.
    That's a rather horrible solution.
    Another solution might be a virtual Delete function.
    Delete calls "delete this", possible also setting the original pointer to NULL.

    You should also take a look at smart pointers!
    There really is little need for raw pointers in C++ anymore.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    lol ur right, the classes in question didnt have deconstructors, ::smacks himself over the head::

    thank god for source control...

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Yeah um you didn't actually listen to anything I said. Perhaps you could go back and read it again.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing derived classes in a stl container
    By *DEAD* in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2008, 07:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM
  4. Replies: 8
    Last Post: 07-27-2003, 01:52 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 02:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21