Rule Definition
If an object is handled through a pointer to its base class (handled through a generic interface that is certainly a major objective in object-oriented programming), memory and stability problems occur when the object is to be deleted and when the object has been created on the heap with new. If the pointer is to the base class, the compiler can only know to call the base-class version of the destructor during the delete. Any memory clean up or functional actions that must take place when derived classes are deleted will not take place provoking memory leaks, resource losses and stability issues.
Remediation
Add a virtual destructor to the base class.
Violation Code Sample
class CParent
{
CParent() { ... }
~CParent() { ... }
};
class CDerived : public CParent
{
...
};
Fixed Code Sample
class CParent
{
CParent() { ... }
virtual ~CParent() { ... }
};
class CDerived : public CParent
{
...
};
Reference
Bruce Eckel's "Thinking in C++"
http://www.cs.huji.ac.il/labs/parallel/Docs/C++/Guide/tic0161.html
Bruce Eckel's "Thinking in C++"
http://www.cs.huji.ac.il/labs/parallel/Docs/C++/Guide/
Related Technologies
C++
Technical Criterion
CWE-1079 - Parent Class without Virtual Destructor Method
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.