C++中的对象销毁

时间:2023-03-07
本文介绍了C++中的对象销毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

C++ 中的对象究竟何时被销毁,这意味着什么?由于没有垃圾收集器,我是否必须手动销毁它们?异常如何发挥作用?

(注意:这是Stack Overflow 的 C++ 常见问题解答的条目.如果您想批评以这种形式提供常见问题解答,然后 开始在元数据上发帖所有这些都可以做到这一点.C++ 聊天室中监控该问题的答案a>,FAQ 的想法最初是从这里开始的,因此您的答案很可能会被提出该想法的人阅读.)

解决方案

在下面的文本中,我将区分作用域对象,它们的销毁时间由它们的封闭作用域(函数、块、类、表达式)和动态对象,它们的确切销毁时间通常要到运行时才能知道.

虽然类对象的销毁语义由析构函数决定,但标量对象的销毁始终是空操作.具体来说,销毁指针变量不会销毁指针对象.

作用域对象

自动对象

当控制流离开其定义的范围时,自动对象(通常称为局部变量")将按照其定义的相反顺序进行销毁:

void some_function(){福一;福 b;如果(some_condition){傅y;福兹;} <--- z 和 y 在这里被破坏} <--- b 和 a 在这里被破坏

如果在函数执行过程中抛出异常,则在异常传播到调用者之前,所有先前构造的自动对象都会被销毁.这个过程称为堆栈展开.在堆栈展开期间,前面提到的自动对象的析构函数不会再有任何异常.否则,函数 std::terminate 被调用.

这引出了 C++ 中最重要的准则之一:

<块引用>

析构函数不应该抛出.

非本地静态对象

在命名空间范围内定义的静态对象(通常称为全局变量")和静态数据成员在执行main后按照其定义的相反顺序被销毁:

struct X{静态 Foo x;//这只是一个*声明*,而不是*定义*};福一;福 b;int main(){} <--- y, x, b 和 a 在这里被破坏福 X::x;//这是各自的定义傅y;

请注意,在不同翻译单元中定义的静态对象的构造(和销毁)的相对顺序是未定义的.

如果异常离开静态对象的析构函数,则调用函数std::terminate.

本地静态对象

在函数内部定义的静态对象是在(如果)控制流第一次通过它们的定义时构造的.1main 执行后,它们以相反的顺序销毁:

Foo&get_some_Foo(){静态 Foo x;返回 x;}酒吧&get_some_Bar(){静态酒吧 y;返回 y;}int main(){get_some_Bar().do_something();//注意 get_some_Bar 被称为 *first*get_some_Foo().do_something();} <--- x 和 y 在这里被破坏//因此 y 被破坏 *last*

如果异常离开静态对象的析构函数,则调用函数std::terminate.

1:这是一个极其简化的模型.静态对象的初始化细节其实要复杂得多.

基类子对象和成员子对象

当控制流离开对象的析构函数体时,其成员子对象(也称为其数据成员")按照其定义的相反顺序进行析构.之后,它的基类子对象按照基类说明符列表的相反顺序销毁:

class Foo : Bar, Baz{库克斯 x;曲y;上市:~Foo(){} <--- y 和 x 在这里被破坏,};其次是 Baz 和 Bar 基类子对象

如果在 Foo 的子对象之一的构造过程中抛出异常,那么在传播异常之前,其先前构造的所有子对象都将被销毁.另一方面,Foo 析构函数将不会被执行,因为 Foo 对象从未被完全构造过.

请注意,析构函数体不负责析构数据成员本身.如果数据成员是对象销毁时需要释放的资源的句柄(例如文件、套接字、数据库连接、互斥锁或堆内存),则只需编写析构函数.

数组元素

数组元素按降序销毁.如果在第 n 个元素的构造过程中抛出异常,则在传播异常之前先销毁第 n-1 到 0 个元素.

临时对象

当评估类类型的纯右值表达式时,会构造一个临时对象.prvalue 表达式最突出的例子是调用按值返回对象的函数,例如 T operator+(const T&, const T&).一般情况下,当词法上包含纯右值的完整表达式被完全求值时,临时对象就会被破坏:

__________________________ 完整表达式___________子表达式_______ 子表达式some_function(a + " " + b);^ 两个临时对象都在这里被破坏

上面的函数调用 some_function(a + " " + b) 是一个完整的表达式,因为它不是更大表达式的一部分(相反,它是表达式语句的一部分).因此,在评估子表达式期间构造的所有临时对象都将在分号处销毁.有两个这样的临时对象:第一个是在第一次加法时构造的,第二个是在第二次加法时构造的.第二个临时对象将在第一个之前销毁.

如果在第二次添加过程中抛出异常,第一个临时对象将在传播异常之前正确销毁.

如果局部引用是用纯右值表达式初始化的,临时对象的生命周期会扩展到局部引用的范围,所以你不会得到一个悬空引用:

<代码>{const Foo&r = a + " " + b;^ 第一个临时 (a + " ") 在这里被破坏//...} <--- 第二个临时 (a + " " + b) 直到这里才被破坏

如果计算非类类型的纯右值表达式,结果是一个,而不是一个临时对象.但是,如果使用纯右值来初始化引用,则构造一个临时对象:

const int&r = i + j;

动态对象和数组

在下一节中,destroy X 的意思是先销毁 X,然后释放底层内存".类似地,create X 的意思是先分配足够的内存,然后在那里构造 X".

动态对象

通过p = new Foo 创建的动态对象通过delete p 销毁.如果你忘记delete p,你就有了资源泄漏.您永远不应尝试执行以下操作之一,因为它们都会导致未定义的行为:

  • 通过delete[](注意方括号)、free 或任何其他方式销毁动态对象
  • 多次销毁动态对象
  • 在动态对象被销毁后访问它

如果在动态对象的构造过程中抛出异常,则在传播异常之前释放底层内存.(析构函数将不会在内存释放之前执行,因为对象从未完全构造过.)

动态数组

通过p = new Foo[n] 创建的动态数组通过delete[] p 销毁(注意方括号).如果你忘记delete[] p,你就有了资源泄漏.您永远不应尝试执行以下操作之一,因为它们都会导致未定义的行为:

  • 通过deletefree或任何其他方式销毁动态数组
  • 多次销毁动态数组
  • 在动态数组被销毁后访问它

如果在第n个元素的构造过程中抛出异常,则按降序销毁n-1到0个元素,释放底层内存,并传播异常.

(对于动态数组,您通常应该更喜欢 std::vector 而不是 Foo*.它使编写正确和健壮的代码变得更加容易.)

引用计数智能指针

一个由多个std::shared_ptr对象管理的动态对象在最后一个参与共享的std::shared_ptr对象销毁过程中被销毁那个动态对象.

(对于共享对象,您通常应该更喜欢 std::shared_ptr<Foo> 而不是 Foo*.它使编写正确和健壮的代码变得更加容易.)

When exactly are objects destroyed in C++, and what does that mean? Do I have to destroy them manually, since there is no Garbage Collector? How do exceptions come into play?

(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)

解决方案

In the following text, I will distinguish between scoped objects, whose time of destruction is statically determined by their enclosing scope (functions, blocks, classes, expressions), and dynamic objects, whose exact time of destruction is generally not known until runtime.

While the destruction semantics of class objects are determined by destructors, the destruction of a scalar object is always a no-op. Specifically, destructing a pointer variable does not destroy the pointee.

Scoped objects

automatic objects

Automatic objects (commonly referred to as "local variables") are destructed, in reverse order of their definition, when control flow leaves the scope of their definition:

void some_function()
{
    Foo a;
    Foo b;
    if (some_condition)
    {
        Foo y;
        Foo z;
    }  <--- z and y are destructed here
}  <--- b and a are destructed here

If an exception is thrown during the execution of a function, all previously constructed automatic objects are destructed before the exception is propagated to the caller. This process is called stack unwinding. During stack unwinding, no further exceptions may leave the destructors of the aforementioned previously constructed automatic objects. Otherwise, the function std::terminate is called.

This leads to one of the most important guidelines in C++:

Destructors should never throw.

non-local static objects

Static objects defined at namespace scope (commonly referred to as "global variables") and static data members are destructed, in reverse order of their definition, after the execution of main:

struct X
{
    static Foo x;   // this is only a *declaration*, not a *definition*
};

Foo a;
Foo b;

int main()
{
}  <--- y, x, b and a are destructed here

Foo X::x;           // this is the respective definition
Foo y;

Note that the relative order of construction (and destruction) of static objects defined in different translation units is undefined.

If an exception leaves the destructor of a static object, the function std::terminate is called.

local static objects

Static objects defined inside functions are constructed when (and if) control flow passes through their definition for the first time.1 They are destructed in reverse order after the execution of main:

Foo& get_some_Foo()
{
    static Foo x;
    return x;
}

Bar& get_some_Bar()
{
    static Bar y;
    return y;
}

int main()
{
    get_some_Bar().do_something();    // note that get_some_Bar is called *first*
    get_some_Foo().do_something();
}  <--- x and y are destructed here   // hence y is destructed *last*

If an exception leaves the destructor of a static object, the function std::terminate is called.

1: This is an extremely simplified model. The initialization details of static objects are actually much more complicated.

base class subobjects and member subobjects

When control flow leaves the destructor body of an object, its member subobjects (also known as its "data members") are destructed in reverse order of their definition. After that, its base class subobjects are destructed in reverse order of the base-specifier-list:

class Foo : Bar, Baz
{
    Quux x;
    Quux y;

public:

    ~Foo()
    {
    }  <--- y and x are destructed here,
};          followed by the Baz and Bar base class subobjects

If an exception is thrown during the construction of one of Foo's subobjects, then all its previously constructed subobjects will be destructed before the exception is propagated. The Foo destructor, on the other hand, will not be executed, since the Foo object was never fully constructed.

Note that the destructor body is not responsible for destructing the data members themselves. You only need to write a destructor if a data member is a handle to a resource that needs to be released when the object is destructed (such as a file, a socket, a database connection, a mutex, or heap memory).

array elements

Array elements are destructed in descending order. If an exception is thrown during the construction of the n-th element, the elements n-1 to 0 are destructed before the exception is propagated.

temporary objects

A temporary object is constructed when a prvalue expression of class type is evaluated. The most prominent example of a prvalue expression is the call of a function that returns an object by value, such as T operator+(const T&, const T&). Under normal circumstances, the temporary object is destructed when the full-expression that lexically contains the prvalue is completely evaluated:

__________________________ full-expression
              ___________  subexpression
              _______      subexpression
some_function(a + " " + b);
                          ^ both temporary objects are destructed here

The above function call some_function(a + " " + b) is a full-expression because it is not part of a larger expression (instead, it is part of an expression-statement). Hence, all temporary objects that are constructed during the evaluation of the subexpressions will be destructed at the semicolon. There are two such temporary objects: the first is constructed during the first addition, and the second is constructed during the second addition. The second temporary object will be destructed before the first.

If an exception is thrown during the second addition, the first temporary object will be destructed properly before propagating the exception.

If a local reference is initialized with a prvalue expression, the lifetime of the temporary object is extended to the scope of the local reference, so you won't get a dangling reference:

{
    const Foo& r = a + " " + b;
                              ^ first temporary (a + " ") is destructed here
    // ...
}  <--- second temporary (a + " " + b) is destructed not until here

If a prvalue expression of non-class type is evaluated, the result is a value, not a temporary object. However, a temporary object will be constructed if the prvalue is used to initialize a reference:

const int& r = i + j;

Dynamic objects and arrays

In the following section, destroy X means "first destruct X and then release the underlying memory". Similarly, create X means "first allocate enough memory and then construct X there".

dynamic objects

A dynamic object created via p = new Foo is destroyed via delete p. If you forget to delete p, you have a resource leak. You should never attempt to do one of the following, since they all lead to undefined behavior:

  • destroy a dynamic object via delete[] (note the square brackets), free or any other means
  • destroy a dynamic object multiple times
  • access a dynamic object after it has been destroyed

If an exception is thrown during the construction of a dynamic object, the underlying memory is released before the exception is propagated. (The destructor will not be executed prior to memory release, because the object was never fully constructed.)

dynamic arrays

A dynamic array created via p = new Foo[n] is destroyed via delete[] p (note the square brackets). If you forget to delete[] p, you have a resource leak. You should never attempt to do one of the following, since they all lead to undefined behavior:

  • destroy a dynamic array via delete, free or any other means
  • destroy a dynamic array multiple times
  • access a dynamic array after it has been destroyed

If an exception is thrown during the construction of the n-th element, the elements n-1 to 0 are destructed in descending order, the underlying memory is released, and the exception is propagated.

(You should generally prefer std::vector<Foo> over Foo* for dynamic arrays. It makes writing correct and robust code much easier.)

reference-counting smart pointers

A dynamic object managed by several std::shared_ptr<Foo> objects is destroyed during the destruction of the last std::shared_ptr<Foo> object involved in sharing that dynamic object.

(You should generally prefer std::shared_ptr<Foo> over Foo* for shared objects. It makes writing correct and robust code much easier.)

这篇关于C++中的对象销毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!