我正在制作一款 iPhone 游戏。我想释放所有已分配或保留的对象。在dealloc函数中,我释放了所有这些对象,但后来我意识到,有时我最终会在对象尚未分配时释放它们。所以我想在释放它之前我需要检查它的 retainCount
是否大于零。
我的问题是:
我是否只检查 retainCount
是否大于零,然后释放它?
if([bg retainCount]!=0)
{
[bg release];
}
或者
我应该释放它的次数与它的 retainCount
一样多
while([bg retainCount]!=0)
{
[bg release];
}
感谢您的帮助!
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.
My question is:
Do I just check if the retainCount
is greater than zero and then release it?
if([bg retainCount]!=0)
{
[bg release];
}
or
Should I release it as many times as its retainCount
while([bg retainCount]!=0)
{
[bg release];
}
Thanks for your help!
发布评论
评论(2)
Autorelease 使得retainCount 变得毫无意义。跟踪保留和保留您是否拥有某个对象。学习与研究记住这些规则: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH
Autorelease makes retainCount meaningless. Keep track of retains & whether you own an object. Study & remember these rules: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-BAJHFBGH
不要使用-retainCount。
对象的绝对保留计数是没有意义的。
您应该调用
release
的次数与导致对象被保留的次数完全相同。不能少(除非你喜欢泄漏),当然也不能多(除非你喜欢崩溃)。请参阅内存管理指南了解完整详细信息。
Do not use -retainCount.
The absolute retain count of an object is meaningless.
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).See the Memory Management Guidelines for full details.