简体   繁体   English

什么时候会释放记忆?

[英]when memory will be released?

I have created a code block, like this. 我已经创建了一个代码块,就像这样。

proc()
{
    Z* z = new Z();
}

now the pointer declared inside method proc will have scope only till proc. 现在,在方法proc中声明的指针将只有范围直到proc。 I want to ask when the DTOR for z will be called automatically. 我想询问何时自动调用DTOR for z。 whether when the controls come out of the method proc or when my application is closed. 是否控件来自方法proc或我的应用程序关闭时。

The destructor will not be called at all. 根本不会调用析构函数。 The memory used by *z will be leaked until the application is closed (at which point the operating system will reclaim all memory used by your process). *z使用的内存将被泄露,直到应用程序关闭(此时操作系统将回收您的进程使用的所有内存)。

To avoid the leak, you have to call delete at some point or, better yet, use smart pointers. 为了避免泄漏,您必须在某些时候调用delete ,或者更好的是,使用智能指针。

This is a memory leak. 这是内存泄漏。 What you probably should have is: 你可能应该拥有的是:

void
proc()
{
    Z z;
}

and skip the dynamic allocation. 并跳过动态分配。 If an object's lifetime corresponds to its scope, you rarely need dynamic allocation. 如果对象的生命周期与其范围相对应,则很少需要动态分配。

If for some reason you do need dynamic allocation (eg because of polymorphism), then you should use some sort of smart pointer; 如果由于某种原因你需要动态分配(例如因为多态),那么你应该使用某种智能指针; std::auto_ptr works well here, and things like scoped_ptr , if you have them, may be even better. std::auto_ptr在这里工作得很好,像scoped_ptr这样的东西,如果你有它们,可能会更好。

This is one of the fundamentals in C++. 这是C ++的基础之一。

Dynamic allocation 动态分配

In your case, memory allocation and consequent constructor call for Z will happen on new : 在你的情况下,内存分配和随后的Z构造函数调用将在new发生:

Z* z = new Z();

The opposite part for destruction and memory deallocation will happen on delete : 销毁和内存释放的相反部分将在delete时发生:

delete z;

But since your code don't have it, memory deallocation will never happen, plus you will lose the pointer z having no possibility deallocating the object in future. 但是由于你的代码没有它,内存释放永远不会发生,而且你将丢失指针z ,以后将来不可能解除分配对象。 This is typical memory leak. 这是典型的内存泄漏。

Declaration 宣言

On the other hand, if you declare object like this: 另一方面,如果你声明这样的对象:

Z z;

Memory allocation and constructor will be called immediately right here at declaration point, and when object's scope of existence is finished (ie at the end of the function) destructor will be called automatically and memory will be deallocated. 内存分配和构造函数将立即在声明点处被调用,当对象的存在范围完成时(即在函数结束时),将自动调用析构函数并释放内存。

Dynamic allocation vs Declaration 动态分配与声明

I will not go into debates about what is better and what is not, but rather will provide the excerpt from one of the articles that is linked below: 我不会讨论什么是更好的,什么不是什么,而是将提供下面链接的一篇文章的摘录:

Unlike declarations, which load data onto the programs data segment, dynamic allocation creates new usable space on the programs STACK (an area of RAM specifically allocated to that program). 与将数据加载到程序数据段的声明不同,动态分配在程序STACK(专门分配给该程序的RAM区域)上创建新的可用空间。

FYI : Stack = Performance , but not always the best solution . 仅供参考: Stack = Performance ,但并非总是最佳解决方案

References 参考

For your pleasure : tic tac toe . 为了您的乐趣: tic tac toe

除非将z传递给delete否则将会发生内存泄漏。

DTOR will not be called automatically. 不会自动调用DTOR。 You should use "delete" keyword. 您应该使用“删除”关键字。

Z的析构函数不会被调用,除非你在代码中输入这样的行:

delete z;

当你使用new时,对象在堆上分配,堆在程序中的所有函数之间共享,即你可以松散地说,堆分配对象的范围是你的程序,所以不对对象进行删除,它将一直存在,直到你的程序退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM