views:

23

answers:

1

What is the purpose of setting things to nil in the viewDidUnload() method? I know it has something to do with releasing memory, but I thought that's what release was for in the dealloca() method. How do you know which things to put in viewDidUnload() to set to nil?

+1  A: 

The purpose is to free up memory that no longer needs to be used (because the view isn't in use) and the things you put there are things that can be re-created on viewDidLoad (generally speaking).

BarrettJ
How does setting something to nil free its memory?
awakeFromNib
It reduces the number of references to it, and once the reference count reaches zero the system knows it can deallocate that memory. For more info on how all this works: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
BarrettJ
BarrettJ: Only if you assign `nil` to the property, because the setter will release the object. If you assign `nil` directly to the instance variable, nothing releases it, so you leak the object. (Naturally, in Cocoa, this only applies to manually-managed code, not code running under GC.)
Peter Hosey
So then is there something else that is constantly keeping track of the number of references for each object?
awakeFromNib