为什么我必须调用super -dealloc最后,而不是第一? [英] Why do I have to call super -dealloc last, and not first?

查看:380
本文介绍了为什么我必须调用super -dealloc最后,而不是第一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确的例子:

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

错误示例:

- (void)dealloc {
    [super dealloc];
    [viewController release];
    [window release];
}

Althoug在所有其他情况下覆盖一个方法我会先调用super方法实现,在这种情况下,apple总是调用[super dealloc]。为什么?

Althoug in alsmost all other cases when overriding a method I would first call the super's method implementation, in this case apple always calls [super dealloc] in the end. Why?

推荐答案

它只是一个指南。您可以在 [super dealloc] 后调用其他指令。但是你不能访问超类的变量,因为当你调用 [super dealloc] 时它们被释放。调用最后一行中的超类总是安全的。

Its just a guideline. You can call other instructions after [super dealloc]. however you can not access variables of the superclass anymore because they are released when you call [super dealloc]. It is always safe to call the superclass in the last line.

此外,KVO和依赖(触发)键如果依赖于已发布的成员变量,则可能产生副作用。

Also KVO and depended (triggered) keys can produce side effects if they are depended of already released member variables.

这篇关于为什么我必须调用super -dealloc最后,而不是第一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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