在被删除的对象内部动态分配对象会发生什么?
当我在对象上使用 delete
关键字时,对象析构函数是否被执行?如果我删除的对象包含其他指针而我不删除它们怎么办,这是内存泄漏吗?
有时我对何时使用删除
感到有点困惑。当我传递东西时,问题对我来说是最糟糕的。我不知道什么时候使用 delete
是安全的,因为担心删除从其他地方指向的对象。
When I use the delete
keyword on an object is that objects destructor executed? What if the objected I deleted contains other pointers and I don't delete them, is that a memory leak?
I get a confused a bit sometimes over when to use delete
. The problem is worst for me when I pass things around. I don't know when it's safe to use delete
for fear of eliminating an object that is being pointed to from somewhere else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的
是的,除非其他人也有一个指针并且其他人负责删除它们。您还可能会因双重删除而遇到问题。如果你删除了给你的一个指针,但其他人有一个指向该内存的指针,那么他的指针现在什么也没有指向。当他使用该指针时,他的程序可能会崩溃。
你不是唯一一个。在代码各部分之间建立关于谁拥有什么的协议和约定非常重要。什么是“生产”对象。是什么“消耗”了他们。您可能还希望使用 boost::shared_ptr 和 boost::weak_ptr 等工具来允许引用计数。
Yes
Yes, unless someone else also has a pointer and that someone else is in charge of deleting them. You can also have problems due to double deletion. If you delete a pointer given to you, but someone else has a pointer to that memory, then his pointer now points at nothing. When he goes to use that pointer, his program could crash.
You're not the only one. Its important to establish protocols and conventions between parts of your code about who owns what. What is "producing" objects. What is "consuming" them. You may also wish to use tools such as boost::shared_ptr and boost::weak_ptr to allow for reference counting.
那是内存泄漏。这是一个非常常见的问题。当您删除使用 new 为其分配内存的对象时,它将调用该对象的析构函数。析构函数是您应该提供清理(删除)在该对象的生命周期内可能分配的所有内存的实现的地方。
That is a memory leak. That's a very common problem. When you delete an object that you used new to allocate memory for, it will call that objects destructor. The destructor is where you are supposed to provide the implementation to clean up (delete) all of the memory you may have allocated during the lifetime of that object.
如果使用new,则必须使用delete。这将触发实例的析构函数被调用。如果该实例在其构造函数(或更高版本)中新建了任何对象,则应在其析构函数中删除它们。
另外,如果您新建数组(new char[20] 等),则在删除时必须使用“delete []”,否则行为未定义。
您可以通过使用 std::tr1::shared_ptr 或 boost::shared_ptr 来避免很多痛苦,它们会进行引用计数并为您执行删除操作,而不是:
do
then you don't need to do删除:当shared_ptr的公共引用计数为零时,它将为您执行删除操作。
If you use new, you must use delete. That will trigger the instance's destructor to be called. If that instance new'd any objects in its constructor (or later) it should delete them in its destructor.
Also, if you new up arrays (new char[20] etc) you must use 'delete []' when deleting, else the behaviour is undefined.
You can avoid a lot of the pain by using std::tr1::shared_ptr, or boost::shared_ptr, which will do reference counting and do the deletes for you, that is instead of:
do
then you don't need to do a delete: when the shared_ptr's communal reference count goes to zero it will do the delete for you.
是的,这是泄漏。查看Freestore 管理常见问题解答是否可以帮助您加深理解。
Yes it is a leak. See if the Freestore management FAQ can add some insight to you understanding.
当你调用delete someObect;时,会发生这样的事情:
操作员删除按照我的理解与free做同样的事情 - 只是释放(释放)的记忆。但请记住,如果您使用了new,则不能使用free,您必须使用delete,并且只能使用它。
When you call delete someObect; happens something like this:
And operator delete do as I understand the same thing that free do - just frees(deallocates) the memory. But remember, that if you used new you cannot use free, you must use delete and only it.
是的。这就是析构函数的用途。
是的。这是最常见的内存泄漏来源之一。
这是一个难题。没有系统可以完美解决这个问题,但是通过使用引用计数智能指针并减少共享对象的数量,您可以取得很大的进展。
Yes. That's what the destructor is for.
Yes. This is one of the most common sources of memory leaks.
This is a hard problem. There is no system that solves it perfectly, but you can get pretty far by using reference-counted smart pointers, and by reducing the number of objects that are shared.