在被删除的对象内部动态分配对象会发生什么?

发布于 2024-11-01 07:48:01 字数 198 浏览 5 评论 0原文

当我在对象上使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

向日葵 2024-11-08 07:48:02

当我在
object 是对象的析构函数
被处决?

是的

如果我删除了反对的内容怎么办
包含其他指针,但我不包含
删除它们,这是内存泄漏吗?

是的,除非其他人也有一个指针并且其他人负责删除它们。您还可能会因双重删除而遇到问题。如果你删除了给你的一个指针,但其他人有一个指向该内存的指针,那么他的指针现在什么也没有指向。当他使用该指针时,他的程序可能会崩溃。

不知道什么时候可以安全使用
删除是因为害怕消除
被指向的对象
其他地方。

你不是唯一一个。在代码各部分之间建立关于谁拥有什么的协议和约定非常重要。什么是“生产”对象。是什么“消耗”了他们。您可能还希望使用 boost::shared_ptr 和 boost::weak_ptr 等工具来允许引用计数。

When I use the delete keyword on an
object is that objects destructor
executed?

Yes

What if the objected I deleted
contains other pointers and I don't
delete them, is that a memory leak?

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.

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.

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.

濫情▎り 2024-11-08 07:48:02

那是内存泄漏。这是一个非常常见的问题。当您删除使用 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.

悲歌长辞 2024-11-08 07:48:02

如果使用new,则必须使用delete。这将触发实例的析构函数被调用。如果该实例在其构造函数(或更高版本)中新建了任何对象,则应在其析构函数中删除它们。

另外,如果您新建数组(new char[20] 等),则在删除时必须使用“delete []”,否则行为未定义。

您可以通过使用 std::tr1::shared_ptr 或 boost::shared_ptr 来避免很多痛苦,它们会进行引用计数并为您执行删除操作,而不是:

Foo *pFoo = new Foo;

do

std::tr1::shared_ptr<Foo> pFoo(new Foo);

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:

Foo *pFoo = new Foo;

do

std::tr1::shared_ptr<Foo> pFoo(new Foo);

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.

夜还是长夜 2024-11-08 07:48:02

是的,这是泄漏。查看Freestore 管理常见问题解答是否可以帮助您加深理解。

Yes it is a leak. See if the Freestore management FAQ can add some insight to you understanding.

剪不断理还乱 2024-11-08 07:48:02

当你调用delete someObect;时,会发生这样的事情:

if(someObect != NULL)
{
someObect->~ClassName();// you did ClassName someObect = new ClassName(); 
operator delete(someObect);
}

操作员删除按照我的理解与free做同样的事情 - 只是释放(释放)的记忆。但请记住,如果您使用了new,则不能使用free,您必须使用delete,并且只能使用它。

When you call delete someObect; happens something like this:

if(someObect != NULL)
{
someObect->~ClassName();// you did ClassName someObect = new ClassName(); 
operator delete(someObect);
}

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.

七月上 2024-11-08 07:48:01

当我在对象上使用delete关键字时,对象的析构函数是否被执行?

是的。这就是析构函数的用途。

如果我删除的对象包含其他指针并且我不删除它们,会发生内存泄漏吗?

是的。这是最常见的内存泄漏来源之一。

我不知道什么时候使用删除是安全的,因为担心删除从其他地方指向的对象。

这是一个难题。没有系统可以完美解决这个问题,但是通过使用引用计数智能指针并减少共享对象的数量,您可以取得很大的进展。

When I use the delete keyword on an object is that objects destructor executed?

Yes. That's what the destructor is for.

What if the objected I deleted contains other pointers and I don't delete them, is that a memory leak?

Yes. This is one of the most common sources of memory leaks.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文