MISRA C++:2023 Rule 21.6.5
Description
Rule Definition
A pointer to an incomplete class type shall not be deleted.
Rationale
When you delete a pointer to an incomplete class, it is not possible to call any nontrivial destructor that the class might have. If the destructor performs cleanup activities such as memory deallocation, these activities do not happen.
A similar problem happens, for instance, when you downcast to a pointer to an incomplete class (downcasting is casting from a pointer to a base class to a pointer to a derived class). At the point of downcasting, the relationship between the base and derived class is not known. In particular, if the derived class inherits from multiple classes, at the point of downcasting, this information is not available. The downcasting cannot make the necessary adjustments for multiple inheritance and the resulting pointer cannot be dereferenced.
Polyspace Implementation
The checker flags situations where you delete or cast to a pointer to an incomplete class. An incomplete class is one whose definition is not visible at the point where the class is used.
For instance, the definition of class Body
is not visible when the
delete
operator is called on a pointer to
Body
:
class Handle { class Body *impl; public: ~Handle() { delete impl; } // ... };
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Language support library |
Category: Required |
Version History
Introduced in R2024b