Main Content

Base class destructor not virtual

Class cannot behave polymorphically for deletion of derived class objects

Description

This defect occurs when a class has virtual functions but not a virtual destructor.

Risk

The presence of virtual functions indicates that the class is intended for use as a base class. However, if the class does not have a virtual destructor, it cannot behave polymorphically for deletion of derived class objects.

If a pointer to this class refers to a derived class object, and you use the pointer to delete the object, only the base class destructor is called. Additional resources allocated in the derived class are not released and can cause a resource leak.

Fix

One possible fix is to always use a virtual destructor in a class that contains virtual functions.

Examples

expand all

class Base {        
        public:
                Base(): _b(0) {};
                virtual void update() {_b += 1;};
        private:
                int _b;
};

class Derived: public Base {                     
        public:
                Derived(): _d(0) {};
                ~Derived() {_d = 0;};
                virtual void update() {_d += 1;};
        private:
                int _d;
};

In this example, the class Base does not have a virtual destructor. Therefore, if a Base* pointer points to a Derived object that is allocated memory dynamically, and the delete operation is performed on that Base* pointer, the Base destructor is called. The memory allocated for the additional member _d is not released.

The defect appears on the base class definition. Following are some tips for navigating in the source code:

  • To find classes derived from the base class, right-click the base class name and select Search For All References. Browse through each search result to find derived class definitions.

  • To find if you are using a pointer or reference to a base class to point to a derived class object, right-click the base class name and select Search For All References. Browse through search results that start with Base* or Base& to locate pointers or references to the base class. You can then see if you are using a pointer or reference to point to a derived class object.

Correction — Make Base Class Destructor Virtual

One possible correction is to declare a virtual destructor for the class Base.

class Base {        
        public:
                Base(): _b(0) {};
                virtual ~Base() {_b = 0;};
                virtual void update() {_b += 1;};
        private:
                int _b;
};

class Derived: public Base {                     
        public:
                Derived(): _d(0) {};
                ~Derived() {_d = 0;};
                virtual void update() {_d += 1;};
        private:
                int _d;
};

Result Information

Group: Object oriented
Language: C++
Default: On for handwritten code, off for generated code
Command-Line Syntax: DTOR_NOT_VIRTUAL
Impact: Medium

Version History

Introduced in R2015b