...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <memory>
struct Base {
virtual void f();
};
struct Derived : Base {};
void f() {
std::unique_ptr<Base> b = std::make_unique<Derived()>unique<Derived>();
} |
Compliant Solution
In this compliant solution, the destructor for Base has an explicitly declared virtual destructor, ensuring that the polymorphic delete operation results in well-defined behavior.
...
Attempting to destruct a polymorphic object that does not have a virtual destructor declared results in undefined behavior. In practice, potential consequences include abnormal program termination and memory leaks.
Rule | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level |
|---|---|---|---|---|---|---|
OOP52-CPP | Low | Likely | No | LowNo | P9 P3 | L2 L3 |
Automated Detection
Tool | Version | Checker | Description | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| non-virtual-public-destructor-in-non-final-class | Partially checked | |||||||||||
| Axivion Bauhaus Suite |
| CertC++-OOP52 | ||||||||||||
| Clang |
| -Wdelete-non-virtual-dtor | ||||||||||||
| CodeSonar |
| LANG.STRUCT.DNVD | delete with Non-Virtual Destructor | Clang | | Include Page | | Clang_V | Clang_V | ||||||
| Helix QAC |
| C++3402, C++3403, C++3404 | ||||||||||||
| Klocwork |
| CL.MLK.VIRTUAL | ||||||||||||
| LDRA tool suite |
| 303 S | Partially implemented | |||||||||||
| Parasoft C/C++test |
| CERT_CPP-OOP52-a | Define a virtual destructor in classes used as base classes which have virtual functions | PRQA QA-C++ | ||||||||||
| Include Page | PRQA QA-C++_V | PRQA QA-C++_V | 3402, 3403, 3404||||||||||||
| Polyspace Bug Finder |
| CERT C++: OOP52-CPP | Checks for situations when a class has virtual functions but not a virtual destructor (rule partially covered) | |||||||||||
| PVS-Studio |
| V599, V689 | ||||||||||||
| RuleChecker |
| non-virtual-public-destructor-in-non-final-class | Partially checked | |||||||||||
| SonarQube C/C++ Plugin |
| S1235 |
...