C and C++


LANG.STRUCT.DNVD : delete with Non-Virtual Destructor

Summary

An object of derived class type C is deleted through a pointer to its base class type B, but B does not have a virtual destructor.

Properties

Class Name delete with Non-Virtual Destructor
Significance reliability
Mnemonic LANG.STRUCT.DNVD
Categories
AUTOSARC++14 AUTOSARC++14:A12-4-1 Destructor of a base class shall be public virtual, public override or protected non-virtual.
CWE CWE:1079 Parent Class without Virtual Destructor Method
  CWE:1087 Class with Virtual Method without a Virtual Destructor
CERT-CPP CERT-CPP:OOP52-CPP Do not delete a polymorphic object without a virtual destructor
JSF++ JSF++:78 All base classes with a virtual function shall define a virtual destructor.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="delete with Non-Virtual Destructor"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

namespace lang_struct_dnvd {

  class BaseNoExplDtor {
    virtual void f();
  public:
    BaseNoExplDtor(){};
  };

  class BaseExplDtor {
    virtual void f();
  public:
    BaseExplDtor(){};
    ~BaseExplDtor(){};
  };

  class BaseExplVirtualDtor {
    virtual void f();
  public:
    BaseExplVirtualDtor(){};
    virtual ~BaseExplVirtualDtor(){};
  };

  class BaseNotPolymorphic {
    void f();
  public:
    BaseNotPolymorphic();
    ~BaseNotPolymorphic();
  };

  class DerivedNoExplDtor : public BaseNoExplDtor { };
  class DerivedExplDtor : public BaseExplDtor { };
  class DerivedExplVirtualDtor : public BaseExplVirtualDtor { };
  class DerivedNotPolymorphic : public BaseNotPolymorphic { };

  class MultiDerived : public BaseNoExplDtor,
                       public BaseExplDtor,
                       public BaseExplVirtualDtor {};

  void useDerivedClasses(){
    BaseNoExplDtor *noexpldtor = new DerivedNoExplDtor();
    BaseExplDtor *expldtor = new DerivedExplDtor();
    BaseExplVirtualDtor *vdtor = new DerivedExplVirtualDtor();
    BaseNotPolymorphic *notpolymorphic = new DerivedNotPolymorphic();

    BaseNoExplDtor *noexpldtor_multi = new MultiDerived();
    BaseExplDtor *expldtor_multi = new MultiDerived();
    BaseExplVirtualDtor *vdtor_multi = new MultiDerived();

    delete noexpldtor;        // 'delete with Non-Virtual Destructor' warning issued here
    delete expldtor;          // 'delete with Non-Virtual Destructor' warning issued here
    delete vdtor;                                    // ok: BaseExplVirtualDtor declares a virtual destructor
    delete notpolymorphic;                           // ok: BaseNotPolymorphic and DerivedNotPolymorphic have no virtual functions so are not polymorphic
    delete noexpldtor_multi;  // 'delete with Non-Virtual Destructor' warning issued here
    delete expldtor_multi;    // 'delete with Non-Virtual Destructor' warning issued here
                                  // ('Misaligned Object' warning also issued)
    delete vdtor_multi;
  }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.