Objective-C 中的对象释放:为什么在 viewDidUnload 和 dealloc 方法中两次释放对象?
我有一个关于 Objective-C 中对象释放的问题。我在“开始iphone 4开发”(第287页)的第9章中看到了一些示例代码。示例代码两次释放对象:都在 viewDidUnload 和 dealloc 方法中。以下是示例代码:
- (void)viewDidUnload {
self.list = nil;
[childController release], childController = nil;}
- (void)dealloc {
[list release];
[childController release];
[super dealloc];}
childController 被声明为 UIViewController 子类的实例。为什么在viewDidUnload和dealloc方法中都释放呢?由于viewDidUnload中已经释放了childController,是否需要在dealloc方法中再次释放它?根据我的理解,我将编写如下代码:
- (void)viewDidUnload {
self.list = nil;
childController = nil;}
- (void)dealloc {
[list release];
[childController release];
[super dealloc];}
谢谢,
山姆
I have a question about object release in objective-c. I saw some sample codes in Chapter 9 of "Beginning iphone 4 Development"(Page 287). The sample code release an object twice: both in viewDidUnload and dealloc method. Here are the sample codes:
- (void)viewDidUnload {
self.list = nil;
[childController release], childController = nil;}
- (void)dealloc {
[list release];
[childController release];
[super dealloc];}
childController is declared as an instance of UIViewController subclass. Why is it released in both viewDidUnload and dealloc method? Since childController is already released in viewDidUnload, is it necessary to release it again in dealloc method? Based my understanding I will write the code like:
- (void)viewDidUnload {
self.list = nil;
childController = nil;}
- (void)dealloc {
[list release];
[childController release];
[super dealloc];}
Thanks,
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是
viewDidUnload
不能保证像dealloc
方法一样每次都被调用。 (检查这个问题)。在
viewDidUnload
中释放对象的原因是为了避免内存泄漏。由于viewDidUnload
在内存不足警告时被调用,因此您确实需要进行清理以避免在这种情况下出现问题。而且在
nil
上调用release不会导致任何问题,因此在dealloc
方法中对保留对象调用release是安全的,假设指针在被释放后设置为nil其他地方(例如示例中的viewDidUnload
)。The problem is
viewDidUnload
is not guaranteed to be called every time likedealloc
method. (check this question).The reason to release objects in
viewDidUnload
is to avoid memory leaks. SinceviewDidUnload
is called when there's a low memory warning, you do want to clean up to avoid troubles in that case.And also calling release on
nil
will not cause any problem, so it is safe to call release on retained objects in yourdealloc
method assuming the pointers are set to nil after been released elsewhere (like inviewDidUnload
in your example).为了优化可用内存,在 UIViewController 中实现惰性获取器(实际上是惰性初始化器)并在 viewDidUnload 中释放可轻松重新分配的对象是一个很好的做法。
一个(简化的)懒惰 getter 是这样的:
所以,在 viewDidUnload 中,我将释放 _footerView,因为我可以稍后毫不费力地检索它。 dealloc方法中释放_footerView,并不是错误,因为:
1) 在目标 c 中可以向 nil 对象发送消息,2) dealloc 不会与 viewDidUnload 同时执行,而是稍后执行
In order to optimize available memory, is a good practice to implement lazy getters (actually lazy initializers) in UIViewControllers and release easily reallocable objects in viewDidUnload.
A (simplified) lazy getter is something like:
so, in the viewDidUnload I will release _footerView, because I can retrieve it later without effort. The release of _footerView in dealloc method, is not an error, because:
1) in objective c is ok to send messages to nil objects, 2) dealloc won't be executed at the same time as viewDidUnload but later
我调查了一下,因为我不确定。您需要知道的一切都在这里:何时我应该在 -(void)viewDidUnload 而不是 -dealloc 中释放对象吗?
基本上,在 viewDidUnload 中,您释放在视图生命周期开始时创建的对象(loadView、viewDid 加载等)。因此,如果您的 viewController 收到内存警告,它将卸载视图并再次重新加载它,然后您的对象将在 viewDidUnload 中释放并在 loadView/viewDidLoad/ect 中再次初始化
I've investigated a little bit because I was not sure. And all you need to know is here: When should I release objects in -(void)viewDidUnload rather than in -dealloc?
Basically, in viewDidUnload you release objects that you've created in the beginning of view's life cycle (loadView, viewDid load and so on). So if your viewController receives memory warning it will unload a view and reload it again and then your objects will be released in viewDidUnload and initialized again in loadView/viewDidLoad/ect