Objective-C 中的对象释放:为什么在 viewDidUnload 和 dealloc 方法中两次释放对象?

发布于 2024-12-02 13:38:01 字数 747 浏览 2 评论 0原文


我有一个关于 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 子类的实例。为什么在viewDidUnloaddealloc方法中都释放呢?由于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2024-12-09 13:38:01

问题是 viewDidUnload 不能保证像 dealloc 方法一样每次都被调用。 (检查这个问题)。

viewDidUnload中释放对象的原因是为了避免内存泄漏。由于 viewDidUnload 在内存不足警告时被调用,因此您确实需要进行清理以避免在这种情况下出现问题。

而且在 nil 上调用release不会导致任何问题,因此在 dealloc 方法中对保留对象调用release是安全的,假设指针在被释放后设置为nil其他地方(例如示例中的 viewDidUnload )。

The problem is viewDidUnload is not guaranteed to be called every time like dealloc method. (check this question).

The reason to release objects in viewDidUnload is to avoid memory leaks. Since viewDidUnload 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 your dealloc method assuming the pointers are set to nil after been released elsewhere (like in viewDidUnload in your example).

撑一把青伞 2024-12-09 13:38:01

为了优化可用内存,在 UIViewController 中实现惰性获取器(实际上是惰性初始化器)并在 viewDidUnload 中释放可轻松重新分配的对象是一个很好的做法。
一个(简化的)懒惰 getter 是这样的:

- (UIView *)footerView {
    if (_footerView) {
        return _footerView;
    }
    UIView *view = [[UIView alloc] initWithFrame:A_FRAME];
    return (_footerView = view);
}

所以,在 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:

- (UIView *)footerView {
    if (_footerView) {
        return _footerView;
    }
    UIView *view = [[UIView alloc] initWithFrame:A_FRAME];
    return (_footerView = view);
}

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

陈独秀 2024-12-09 13:38:01

我调查了一下,因为我不确定。您需要知道的一切都在这里:何时我应该在 -(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文