使用viewDidUnload在iPhone上进行内存管理 [英] memory management on the iPhone with viewDidUnload
问题描述
如果在viewDidUnload中将属性设置为nil,是否需要在dealloc中再次释放它?
If you set a property to nil in viewDidUnload do you need to release it again in dealloc?
推荐答案
否,但是:
- 您不需要检查这种情况.
[无发布]
很好. - 您不能指望
viewDidUnload
被调用.
- You don't need to check for that case.
[nil release]
is fine. - You can't count on
viewDidUnload
being called.
因此,只需在 -dealloc
中正常释放即可.
So just release as normal in -dealloc
.
当然,您必须确保实际释放了先前的对象.如果您使用综合设置器,则可以隐式执行 :
Of course, you must make sure that you actually released the previous object. You implicitly do this if you used the synthesized setter:
self.myProperty = nil; // good
// or
[self setMyProperty:nil]; // also good
但是将ivar设置为nil将会泄漏:
But setting the ivar to nil will leak:
self->myProperty = nil; // leaky as a sieve
// or
myProperty = nil; // as useful as a screen door on a submarine
这些是常见错误.
还要注意,在 -dealloc
中将属性设置为nil是一个坏主意.正如肯德尔(Kendall)在评论中指出的那样,您可能会意外地调用KVO行为.在属性中有更全面的讨论在dealloc中:release然后设置为nil?或简单地释放.
Also note that setting properties to nil in -dealloc
is a bad idea. As Kendall points out in the comments, you may unexpectedly invoke KVO behavior. There's a fuller discussion at Properties in dealloc: release then set to nil? or simply release.
这篇关于使用viewDidUnload在iPhone上进行内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!