清理内存泄漏 [英] Cleaning up memory leaks

查看:72
本文介绍了清理内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我在类中使用的set< Item *,Item是我的一个类。在析构函数
中,我想遍历集合并删除元素,所以

没有内存泄漏。这是正确的方法吗?


for(set< Item *> :: iterator iterator = ItemSet.begin(); iterator!=

ItemSet.end(); iterator ++)

{

delete * iterator;

}

I have a set<Item*that I use in a class, Item being one of my classes. In
the destructor, I want to iterate through the set and remove the elements so
there are no memory leaks. Is this the correct way to do it?

for( set<Item*>::iterator iterator = ItemSet.begin(); iterator !=
ItemSet.end(); iterator++)
{
delete *iterator;
}

推荐答案

" Ook" < Ook不要发送任何怪物''垃圾邮件在zootal dot com删除

不要发给我任何怪物''spamwrote:
"Ook" <Ook Don''t send me any freakin'' spam at zootal dot com delete the
Don''t send me any freakin'' spamwrote:

我有一个< Item *我在一个类中使用,Item是我的一个类。

在析构函数中,我想迭代整个集合并删除

元素因此没有内存泄漏。这是正确的方法吗?


for(set< Item *> :: iterator iterator = ItemSet.begin(); iterator!=

ItemSet.end(); iterator ++)

{

delete * iterator;

}
I have a set<Item*that I use in a class, Item being one of my classes.
In the destructor, I want to iterate through the set and remove the
elements so there are no memory leaks. Is this the correct way to do it?

for( set<Item*>::iterator iterator = ItemSet.begin(); iterator !=
ItemSet.end(); iterator++)
{
delete *iterator;
}



对我来说很好看。如果你使用自定义比较

谓词来定义订单的顺序,那么就会出现一个可能的问题。

不是指针。在这种情况下,从集合中删除指针

最好按如下方式完成:保存指针,将其从集合中删除,然后

删除保存的指针。您是否遇到过以上代码

的任何问题?例如,valgrind会抱怨吗?

最好


Kai-Uwe Bux

Looks good to me. One possible problem arises if you use a custom comparison
predicate for the set that defines the order in terms of the pointees and
not in terms of the pointers. In that case, erasing a pointer from the set
is best done as follows: save the pointer, erase it from the set, and then
delete the saved pointer. Did you encounter any problems with the code
above? e.g., does valgrind complain?
Best

Kai-Uwe Bux




" Kai-Uwe Bux" < jk ******** @ gmx.netwrote in message

news:et ********** @ murdoch.acc.Virginia.EDU ...

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:et**********@murdoch.acc.Virginia.EDU...

" Ook" < Ook不要发送任何怪物''垃圾邮件在zootal dot com删除

不要发给我任何怪物''spamwrote:
"Ook" <Ook Don''t send me any freakin'' spam at zootal dot com delete the
Don''t send me any freakin'' spamwrote:

>我有一个< Item *,我在一个类中使用,Item是我的一个类。
在析构函数中,我想迭代整个集合并删除
元素因此没有内存泄漏。这是正确的方法吗?

for(set< Item *> :: iterator iterator = ItemSet.begin(); iterator!=
ItemSet.end(); iterator ++ )
{
删除* iterator;
}
>I have a set<Item*that I use in a class, Item being one of my classes.
In the destructor, I want to iterate through the set and remove the
elements so there are no memory leaks. Is this the correct way to do it?

for( set<Item*>::iterator iterator = ItemSet.begin(); iterator !=
ItemSet.end(); iterator++)
{
delete *iterator;
}



对我来说很好看。如果您使用自定义

比较

谓词来定义订单中的订单并且

不在,则会出现一个可能的问题指针的术语。在这种情况下,从集合中删除指针

最好按如下方式完成:保存指针,将其从集合中删除,然后

删除保存的指针。您是否遇到过以上代码

的任何问题?例如,valgrind会抱怨吗?


最好


Kai-Uwe Bux


Looks good to me. One possible problem arises if you use a custom
comparison
predicate for the set that defines the order in terms of the pointees and
not in terms of the pointers. In that case, erasing a pointer from the set
is best done as follows: save the pointer, erase it from the set, and then
delete the saved pointer. Did you encounter any problems with the code
above? e.g., does valgrind complain?
Best

Kai-Uwe Bux



实际上,当我用一套它运行时,它运行起来,当我用另一套运行它时,它在〜项目中给出了一个未处理的异常错误。对于Item类,我没有析构函数

,也许我需要一个,因为一些Item类中有一些

集,我需要删除那些集在Item析构函数中?


我可以做set.clear()吗?或者我应该set.clear,然后删除

集?

Actually, when I ran it with one set it worked, when I ran it with the other
set it gave an unhandled exception error in ~Item. I don''t have a destructor
for the Item class, maybe I need one there because some Item classes have
sets in them, and I need to delete there sets in the Item destructor?

Can I just do set.clear()? Or should I do set.clear, and then delete the
set?


" Ook" < Ook不要发送任何怪物''垃圾邮件在zootal dot com删除

不要发送给我任何怪胎''垃圾邮件在消息中

news :a7 ****************************** @ giganews.com ...
"Ook" <Ook Don''t send me any freakin'' spam at zootal dot com delete the
Don''t send me any freakin'' spamwrote in message
news:a7******************************@giganews.com ...

>

" Kai-Uwe Bux" < jk ******** @ gmx.netwrote in message

news:et ********** @ murdoch.acc.Virginia.EDU ...
>
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:et**********@murdoch.acc.Virginia.EDU...

>" Ook" < Ook不要发送任何怪物''垃圾邮件在zootal dot com删除
不要发送给我任何怪物''spamwrote:
>"Ook" <Ook Don''t send me any freakin'' spam at zootal dot com delete the
Don''t send me any freakin'' spamwrote:

>>我有一个< Item *,我在一个类中使用,Item是我的一个类。
在析构函数中,我想遍历集合并删除
元素所以没有内存泄漏。这是正确的方法吗?

for(set< Item *> :: iterator iterator = ItemSet.begin(); iterator!=
ItemSet.end(); iterator ++ )
{
删除* iterator;
}
>>I have a set<Item*that I use in a class, Item being one of my classes.
In the destructor, I want to iterate through the set and remove the
elements so there are no memory leaks. Is this the correct way to do it?

for( set<Item*>::iterator iterator = ItemSet.begin(); iterator !=
ItemSet.end(); iterator++)
{
delete *iterator;
}


对我来说很好看。如果您使用自定义的比较
谓词来定义订单中的顺序,而不是根据指针定义订单,则会出现一个可能的问题。在这种情况下,从
集中删除指针最好按如下方式完成:保存指针,将其从集合中删除,然后
然后删除保存的指针。您是否在上面的代码中遇到任何问题?例如,valgrind会抱怨吗?

最好

Kai-Uwe Bux


Looks good to me. One possible problem arises if you use a custom
comparison
predicate for the set that defines the order in terms of the pointees and
not in terms of the pointers. In that case, erasing a pointer from the
set
is best done as follows: save the pointer, erase it from the set, and
then
delete the saved pointer. Did you encounter any problems with the code
above? e.g., does valgrind complain?
Best

Kai-Uwe Bux



实际上,当我运行它时一套它工作,当我用

其他套件运行它时它在〜项目中给出了一个未处理的异常错误。对于Item类,我没有一个

析构函数,也许我需要一个因为一些Item

类已经设置了它们,我需要删除那些集合在项目中

析构函数?


我可以做set.clear()吗?或者我应该set.clear,然后删除

设置?


Actually, when I ran it with one set it worked, when I ran it with the
other set it gave an unhandled exception error in ~Item. I don''t have a
destructor for the Item class, maybe I need one there because some Item
classes have sets in them, and I need to delete there sets in the Item
destructor?

Can I just do set.clear()? Or should I do set.clear, and then delete the
set?



好​​吧,只要你的Item类没有用new或

malloc分配内存,那么你就不需要了用户定义的析构函数。默认的

析构函数会破坏集合(只要它设置的不是指针

本身就应该没问题。)


我不使用套装,但是我完全按照你描述的方式删除了矢量没有

问题,但是矢量没有排序。但是,为什么你会使用一组

指针?按指针类型排序有什么用,你用适当的

编码你不应该试着把同一个指针推两次。所以也许你在Kai-Uwe建议的时候使用自定义比较谓词?

Well, as long as your Item class doesn''t allocate memory itself with new or
malloc then you shouldn''t need a user defined destructor. The default
destructor will destroy the set (as long as it''s a set not of pointers
itself you should be okay).

I don''t use sets, but I delete vectors exactly the way you describe with no
problems, but vectors aren''t sorted. Why, however, would you use a set of
pointers? What good is it sorting by pointer type, and you with proper
coding you shouldn''t ever try to push the same pointer twice. So maybe you
are using a custom comparison predicate as Kai-Uwe suggests?


这篇关于清理内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆