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
2010-07-30 13:38:35
How does setting something to nil free its memory?
awakeFromNib
2010-07-30 13:40:04
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
2010-07-30 13:51:48
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
2010-07-30 23:17:25
So then is there something else that is constantly keeping track of the number of references for each object?
awakeFromNib
2010-08-08 11:03:11