Pointers to incomplete class types shall not be deleted
Pointers to incomplete class types shall not be deleted.
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.
The check raises a defect when 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; }
// ...
};If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: Expressions |
| Category: Required, Automated |