Virtual functions but non-virtual destructor

This is a discussion on Virtual functions but non-virtual destructor within the C++ Programming forums, part of the General Programming Boards category; This is from code I downloaded. It has several classes with virtual functions but no virtual destructors. When I compile ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Virtual functions but non-virtual destructor

    This is from code I downloaded.

    It has several classes with virtual functions but no virtual destructors. When I compile it, it does compile, but it gives a pageful of warning messages about "X has virtual functions but non-virtual destructor."

    I guess the solution would be to add virtual destructors to each of those classes.

    But why is the compiler griping about there being no virtual destructors? Isn't garbage clean up for classes supposed to be automatic in C++?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Without a virtual destructor, the code;
    Code:
        BaseClass *x = new DerivedClass;   // DerivedClass is derived from BaseClass
        delete x;                                            //   undefined behaviour here
    yields undefined behaviour. The existance of virtual member functions in the base class implies that there may be a derived class, and the above is a pretty common usage pattern in practice. Net result is that, if a class has any virtual member functions, it is usually a good idea for it to also have a virtual destructor.

    Garbage cleanup is not automatic when you do dynamic memory allocation: it is necessary to explicitly (and correctly) release dynamically allocated objects.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is a warning, not an error? So you can't say it doesn't compile...

    If BaseClass doesn't have a virtual destructor (in the above case) it means, that if you delete x, the destructor will be called only for BaseClass, but not for DerivedClass. You give delete a BaseClass pointer and this and only this object is going to be cleaned up.

    I'm not sure if there is anything undefined about that, but it surely can lead to problems. For example, if DerivedClass allocates some resources and expects to clean them up in the destructor, this simply won't happen, leading to a memory leak.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I'm not sure if there is anything undefined about that
    Technically speaking it is undefined behavior (according to the standard) when you delete a base class pointer that points to a derived class and the base class destructor is not virtual. In practice, it often means that the derived class destructor is not called, but assuming that's the only issue isn't portable or wise.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Makes sense. Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Covariance with virtual functions and stl containers
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2009, 08:26 AM
  2. overhead of virtual functions
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2009, 12:56 PM
  3. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  4. A virtual destructor dilema
    By tzakieta in forum C++ Programming
    Replies: 10
    Last Post: 09-11-2008, 10:22 AM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 02:26 PM

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