Main Content

CERT C++: OOP52-CPP

Do not delete a polymorphic object without a virtual destructor

Description

Rule Definition

Do not delete a polymorphic object without a virtual destructor.1

Polyspace Implementation

The rule checker checks for Base class destructor not virtual.

Examples

expand all

Issue

Base class destructor not virtual 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.

Example - Base Class Destructor Not Virtual
class Base {         //Noncompliant
        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;
};

Check Information

Group: 09. Object Oriented Programming (OOP)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.