☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

在对象生命周期结束后调用其析构函数

C++-Undefined-Behavior-59
目录 › next › previous

示例:

struct T { ~T(); };

void foo() {
    T obj;
    obj.~T();  // End of lifetime
}              // Undefined behavior

显式调用 obj 的析构函数后,obj 的生命周期结束,但函数返回前还会调用 obj 的析构函数,导致未定义的行为。

又如:

struct B { .... };
struct D: B { .... };

D* p = new D;
p->B::~B();    // End of lifetime of base class object
delete p;      // Undefined behavior

调用 p->B::~B() 后,基类对象的生命周期结束,但在 delete p 时基类析构函数仍会被执行,导致未定义的行为。

依据

ISO/IEC 14882:2003 12.4(14)-undefined ISO/IEC 14882:2011 12.4(15)-undefined

规则

ID_explicitDtorCall ID_this_deleteInDestructor
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.