我释放了多少次分配或保留的对象? [英] How many times do I release an allocated or retained object?

查看:24
本文介绍了我释放了多少次分配或保留的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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