iPhone - dealloc - 发布与零 [英] iPhone - dealloc - Release vs. nil
问题描述
想知道有经验的人是否可以解释一下这个问题。我见过......的例子。
Wondering if someone with experience could possibly explain this a bit more. I have seen examples of...
[view release];
view = nil;
....(void)dealloc。
....inside the (void) dealloc.
有什么不同,哪一个比另一个好?
最好的方法是什么?
What is the difference and is one better then the other? What is the best way?
在进行retainCount测试时,我个人已经看到nil为我减少了3到0的计数,但是只发布了3到2。
When doing retainCount testing I have personally seen nil drop a count from 3 to 0 for me, but release only drops it from 3 to 2.
推荐答案
你所看到的可能就是:
1) [foo release];
2) self.bar = nil;
3) baz = nil;
-
释放对象,通过实例变量访问它
FOO
。实例变量将成为悬空指针。这是dealloc中的首选方法。
Is releasing the object, accessing it through the instance variable
foo
. The instance variable will become a dangling pointer. This is the preferred method in dealloc.
将 nil
分配给属性 bar
on self,这将实际释放当前保留的财产。如果你有一个属性的自定义setter,那么这样做是为了清理不只是支持属性的实例变量。
Is assigning nil
to a property bar
on self, that will in practice release whatever the property is currently retaining. Do this if you have a custom setter for the property, that is supposed to cleanup more than just the instance variable backing the property.
将覆盖指针 baz
使用nil引用对象,但不释放对象。结果是内存泄漏。永远不要这样做。
Will overwrite the pointer baz
referencing the object with nil, but not release the object. The result is a memory leak. Never do this.
这篇关于iPhone - dealloc - 发布与零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!