Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFCCCC
langcpp
#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

-Wdelete-non-virtual-dtor

3402, 3403, 3404

Tool

Version

Checker

Description

Astrée

Include Page
Astrée_V
Astrée_V

non-virtual-public-destructor-in-non-final-class
Partially checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC++-OOP52
Clang
Include Page
Clang_V
Clang_V
-Wdelete-non-virtual-dtor
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

LANG.STRUCT.DNVD

delete with Non-Virtual Destructor

Clang
Include Page
Clang_VClang_V
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C++3402, C++3403, C++3404


Klocwork
Include Page
Klocwork_V
Klocwork_V

CL.MLK.VIRTUAL
CWARN.DTOR.NONVIRT.DELETE


LDRA tool suite
Include Page
LDRA_V
LDRA_V

303 S

Partially implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

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++_VPRQA QA-C++_V

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C++: OOP52-CPPChecks for situations when a class has virtual functions but not a virtual destructor (rule partially covered)
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V599, V689
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V
non-virtual-public-destructor-in-non-final-class
Partially checked
SonarQube C/C++ Plugin
Include Page
SonarQube C/C++ Plugin_V
SonarQube C/C++ Plugin_V
S1235

...