Follow up viewDidUnload vs. dealloc question...

Posted by entaroadun on Stack Overflow See other posts from Stack Overflow or by entaroadun
Published on 2010-05-16T15:48:48Z Indexed on 2010/05/16 15:50 UTC
Read the original article Hit count: 403

Clarification question as a follow up to:

http://stackoverflow.com/questions/2261972/what-exactly-must-i-do-in-viewdidunload http://stackoverflow.com/questions/1158788/when-should-i-release-objects-in-voidviewdidunload-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 in dealloc and release/nil it if it's not nil?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone