跟进viewDidUnload与dealloc问题 [英] Follow up viewDidUnload vs. dealloc question

查看:99
本文介绍了跟进viewDidUnload与dealloc问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

澄清问题作为后续行动:

Clarification question as a follow up to:

我究竟必须在viewDidUnload中做些什么?
我什么时候应该在 - (void)viewDidUnload而不是-dealloc中释放对象?

因此,假设内存错误较​​少,视图被隐藏,并调用viewDidUnload。我们做发布和零舞蹈。之后不需要整个视图堆栈,因此调用dealloc。因为我已经在viewDidUnload中有release和nil,所以我没有在dealloc中使用它。完美。

So let's say there's a low memory error, and the view is hidden, and viewDidUnload is called. We do the release and nil dance. Later the entire view stack is not needed, so dealloc is called. Since I already have the release and nil stuff in viewDidUnload, I don't have it in dealloc. Perfect.

但如果没有低内存错误,则永远不会调用viewDidUnload。 dealloc被调用,因为我没有release和nil的东西,所以有内存泄漏。

But if there's no low memory error, viewDidUnload is never called. dealloc is called and since I don't have the release and nil stuff, there's a memory leak.

换句话说,将调用dealloc而不调用viewDidUnload ?

In other words, will dealloc ever be called without viewDidUnload being called first?

实际的后续操作是,如果我在viewDidLoad中分配并设置了一些东西,并且我将它释放并在viewDidUnload中设置为nil,我是否将其遗漏dealloc,或者我在dealloc做一个防御nil检查,如果它不是nil则释放/ nil吗?

And the practical follow up to that is, if I alloc and set something in viewDidLoad, and I release it and set to nil in viewDidUnload, do I leave it out of dealloc, or do I do a defensive nil check in dealloc and release/nil it if it's not nil?

推荐答案

发布它同时。你不需要检查零。在nil上发布什么都不做。

Release it in both. You don't need to check for nil. Release on a nil does nothing.

然而在viewDidUnload中,如果视图再次加载,则不会释放在viewDidLoad中无法轻松重新创建的ivars。

However in viewDidUnload don't release ivars that you can't easily recreate in viewDidLoad, in case the view becomes loaded again.

通常(没有内存警告)viewDidUnload不被调用,只调用dealloc。

Normally (no memory warnings) viewDidUnload doesn't gets called, only dealloc is called.

这篇关于跟进viewDidUnload与dealloc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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