0

我正在阅读 Apple 发布的LazyTableImages 代码,他们为此做了一些事情(在 NSOperation 子类中):

- (void)dealloc {
    [myProperty release];
    [myProperty2 release];
}

- (void)main {
    //
    // Parse operation undertaken here
    //
    self.myProperty = nil;
    self.myProperty2 = nil;
}

我的想法是,如果 dealloc 在将属性设置为 nil 之前调用它们,他们会这样做。

我的想法在这里正确吗?发布是不必要的,因为self.myProperty = nil有效发布myProperty

我在这段代码中注意到的一件事是它们并没有release全部保留 中的对象dealloc,只有其中一些,这确实是我感到困惑的原因。

干杯

4

1 回答 1

4

你不能做self.myProperty = nil,甚至[myProperty release]之后dealloc。知道为什么? 因为self已经不存在了。

至于您的问题,似乎dealloc不需要发布,但在您的方法中清理实例变量仍然是一种好习惯dealloc

编辑

正如彼得在评论中指出的那样,如果该-main方法从未执行过,那么有-dealloc必要使用 release 语句;没有它们,您将泄漏内存。

于 2011-01-08T22:28:03.320 回答