我释放一个分配或保留的对象多少次? [英] How many times do I release an allocated or retained object?
问题描述
我在做一个iPhone游戏。我想释放所有已分配或保留的对象。在 dealloc
函数中,我释放所有这些对象,但是我意识到有时我最终释放对象,当它们还没有被分配。所以我想我需要检查其 retainCount
是否大于零或在我释放之前。
I am making an iPhone game. I want to release all objects that have been allocated or retained. In the dealloc
function I am releasing all such objects, but then I realized that sometimes I end up releasing objects when they have not been allocated yet. So I figured I need to check if its retainCount
is greater than zero or not before I release it.
我的问题是:
我只是检查 retainCount
是否大于零,然后释放它? >
Do I just check if the retainCount
is greater than zero and then release it?
if([bg retainCount]!=0)
{
[bg release];
}
或
我应该释放它的次数 retainCount
while([bg retainCount]!=0)
{
[bg release];
}
感谢您的帮助!
推荐答案
不要使用-retainCount。
对象的绝对保留计数是无意义的。
Do not use -retainCount.
The absolute retain count of an object is meaningless.
您应该调用 release
与保存对象的次数完全相同。
You should call release
exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).
查看内存管理指南完整详细信息。
这篇关于我释放一个分配或保留的对象多少次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!