开发者

Follow up viewDidUnload vs. dealloc question

Clarification question as a follow up to:

What exactly must I do in viewDidUnload? When should I release objects in -(void)viewDidUnload rather than in -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.

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.

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

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 开发者_如何学Cin dealloc and release/nil it if it's not nil?


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

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜