JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


LANG.STRUCT.VCALL_IN_DTOR : Virtual Call in Destructor

要旨

A virtual member function is called from a destructor.

This can lead to unexpected and potentially dangerous behavior. During execution of a base class B's destructor, when not in an even deeper base class's destructor, any virtual calls through the this pointer will resolve as if B is the dynamic type (most derived type) of this.

For inline destructors, a warning will only be issued if the destructor is invoked at least once.

プロパティ

クラス名 Virtual Call in Destructor
日本語クラス名 Virtual Call in Destructor
クラス分類 信頼性 (reliability)
ニーモニック LANG.STRUCT.VCALL_IN_DTOR
カテゴリー
AUTOSARC++14 AUTOSARC++14:M12-1-1 An object's dynamic type shall not be used from the body of its constructor or destructor.
MisraC++2008 MisraC++2008:12-1-1 An object's dynamic type shall not be used from the body of its constructor or destructor.
MisraC++2023 MisraC++2023:15.1.1 An object's dynamic type shall not be used from within its constructor or destructor
CERT-CPP CERT-CPP:OOP50-CPP Do not invoke virtual functions from constructors or destructors
JSF++ JSF++:70.1 An object shall not be improperly used before its lifetime begins or after its lifetime ends.
  JSF++:71.1 A class's virtual functions shall not be invoked from its destructor or any of its constructors.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Virtual Call in Destructor"

namespace lang_struct_vcall_in_dtor {

  // Code with implicit inline destructor invocation. 

  class BaseExplicit {
      virtual void virtual_cleanup(void) {}
    public:
      ~BaseExplicit() {
          this->virtual_cleanup(); // 'Virtual Call in Destructor' warning issued here.
                                   //   This call to virtual_cleanup() will always invoke BaseExplicit::virtual_cleanup(),
                                   //   which is probably not what the author intends.
      }
  };

  class DerivedExplicit : BaseExplicit {
      int  *iptr;
      virtual void virtual_cleanup(void) override {delete iptr;}
    public:
      DerivedExplicit() {iptr = new int;}
  };

  void use_base_explicit(BaseExplicit *be){
      delete be;                                        // Explicit invocation of inline destructor.
  }

  // Code with implicit destructor invocation. 

  class BaseImplicit {
      virtual void virtual_cleanup(void) {}
    public:
      ~BaseImplicit() {
          this->virtual_cleanup(); // Two 'Virtual Call in Destructor' warnings issued here.
                                   // - one for the virtual call in BaseImplicit::~BaseImplicit()
                                   // - one for the virtual call in DerivedImplicit::~DerivedImplicit()
      }
  };

  class DerivedImplicit : BaseImplicit {
      int *iptr;
      virtual void virtual_cleanup(void) override {delete iptr;}
    public:
        DerivedImplicit() {iptr = new int;  *iptr=5;}
        int value(void){return *iptr;}
  };

  int use_derived_implicit(void){
      DerivedImplicit di;
      return di.value();
                                                        // Implicit invocation of inline DerivedImplicit destructor
                                                        // (and then BaseImplicit destructor) when di goes out of scope.
  }
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。

 

To report problems with this documentation, please visit https://support.codesecure.com/.