我要释放多少次已分配或保留的对象?

发布于 2024-09-20 00:06:03 字数 428 浏览 11 评论 0 原文

我正在制作一款 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

筑梦 2024-09-27 00:06:11

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

堇色安年 2024-09-27 00:06:10

不要使用-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.

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