C++析构函数:什么时候释放内存?
如果我删除一个导致其析构函数被调用的对象,那么内存是在析构函数完成函数中的任何操作之前还是之后被释放?
If I delete an object which causes its destructor to be called, does the memory get freed before or after the destructor has finished doing whatever there is in the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
仅当最小派生类子对象被销毁后才会释放内存。因此,如果您有:
那么首先销毁
Derived
,然后销毁Base
,然后才释放内存。Memory is only freed once the least derived class subobject has been destroyed. So if you have:
then first
Derived
is destroyed, thenBase
is destroyed and only then memory is deallocated.将
delete
分解为它实际在做什么,可以比较清楚地看到内存什么时候被删除。所以像这样的语句:大致相当于这个伪代码:
所以在调用析构函数后内存被释放。析构函数的确切作用不是由删除运算符决定的,而是由类的定义决定的。通常它会进行本地清理并确保其基类析构函数也被调用。
重要的是要认识到释放内存实际上并不是析构函数的一部分。释放内存的是
delete
操作符。请注意,伪代码中的
free
函数实际上是operator delete()
函数之一,无论是针对已删除的类,还是全局的。这实际上释放了内存。Decompose
delete
into what it is actually doing and it is relatively clear to see when the memory is deleted. So a statement like this:Is roughly equivalent to this pseudo-code:
So the memory is freed after the call to the destructor. Exactly what the destructor does is not determined by the
delete
operator, but rather the definition of the class. Usually it does local cleanup and ensures that its base class destructors are also called.It is important to realize that freeing the memory is not actually part of the destructor. It is the
delete
operator which frees the memory.Note that the
free
function in pseudo-code is actually one of theoperator delete()
functions, either for the deleted class, or global. That actually frees up the memory.析构函数完成后,内存将被释放。否则,访问析构函数内的成员变量将导致段错误。
The memory gets freed after the destructor has finished. Otherwise, accessing member variables inside the destructor would cause segfaults.
运算符delete在析构函数之后调用,但是何时释放内存取决于使用的分配器
operator delete is called after destructor, but when the memory is freed is up to used allocator
我认为析构函数本身执行完毕后内存就会被释放。我知道,当捕获异常时,直到对象本身超出范围时才会调用对象的析构函数。
I would think that the memory is freed after the destructor function itself has finished executing. I know that when an exception is caught, the destructor to the object is not called until the object itself goes out of scope.
在 C++ 中,销毁是使用对象中可用的数据执行一些代码。这段代码是任意的。
释放内存是一种低级处理,通常被删除操作符隐藏,永远不应该在调用析构函数之前调用它。
Allocator 接口对此进行了最好的总结:
allocate
和deallocate
用于操作原始内存construct
和destroy
用于调用对象的构造函数和析构函数确切地说,
>construct
、destroy
和deallocate
只能在该分配器先前分配的内存上执行。它还明确指出destroy
不会释放内存,并且需要随后调用deallocate
。请注意,这是一个低级接口,它允许销毁一个对象并重用释放的空间来构造另一个对象。
In C++, destruction is about executing some code using the data available in the object. This code is arbitrary.
Free'ing the memory is a low level handling, hidden by the
delete
operator in general, that should never be called prior to calls to the destructor.This is best summarized by the Allocator interface:
allocate
anddeallocate
are used to manipulate raw memoryconstruct
anddestroy
are used to call the constructors and destructors of the objectsIt is precised that
construct
,destroy
anddeallocate
should only be executed on memory previously allocated by that allocator. It also precises thatdestroy
does not deallocate the memory, and that a subsequent call todeallocate
will be necessary.Note that this is a low-level interface, which allow destroying an object and reusing the freed space to construct another in place.