Rule Definition
A missing virtual keyword in front of a destructor "overriding" a virtual destructor will hide the polymorphic nature of the destructor from developers using the class. They may not know that at execution time other destructors in the inheritance tree will be executed. A missing virtual keyword may also be an indication that the author of the destructor ignored the fact that it needed to be virtual and thus was not aware that the destructor requires specific attention and specific coding.
Remediation
Add the virtual keyword in the destructor declaration, checks that the destructor is implemented as expected and matches the class hierarchy design requirements, and documents the destructor for future extension of the class hierarchy and modification of the method.
Alternatively, if the derived class destructor is empty, remove it.
Violation Code Sample
class Vehicle
{
public:
Vehicle();
virtual ~Vehicle();
void start();
void stop();
virtual void run();
protected:
Engine* theEngine;
};
class Car : public Vehicle
{
public:
Car();
~Car(); // VIOLATION
protected:
int numberOfWheels;
};
Fixed Code Sample
class Car : public Vehicle
{
public:
Car();
virtual ~Car(); // FIXED
protected:
int numberOfWheels;
};
Related Technologies
C++
Technical Criterion
CWE-1045 - Parent Class with a Virtual Destructor and a Child Class without a Virtual Destructor
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.