Take the 2-minute tour ×

Using xcode 4.2 for iPhone app, without ARC ---

When I create an outlet using the interface builder xcode adds two lines of code to my viewController. One in viewDidUnload: -- [self setMyOutlet:nil] and second in dealloc -- [myOutlet release].

I understand the latter (the release). But why set the outlet to nil in viewDidUnload. Doesn't viewDidUnload get called before dealloc and won't setting the outlet to nil negate the release operation in dealloc? Setting to nil makes sense I would think for building a Mac application which is using garbage collection -- but it doesn't make sense for an iPhone app.

Why does the interface builder do this? Should I delete the lines which set the outlets to nil?

share|improve this question
I don't know if you are right, but how about putting a breakpoint in that functions to figure it out? –  Seega Apr 16 '12 at 18:58
possible duplicate of Why assign nil to IBOutlets in viewDidUnload? –  Josh Caswell Apr 16 '12 at 19:07

2 Answers 2

up vote 3 down vote accepted

viewDidUnload may be called and may be not called. It depends on the current memory usage. dealloc is a place where you should clean all your properties (like arrays, custom objects). In viewDidUnload you clean views and perhaps objects created to support the view. viewDidUnload mean that your view is unloaded (but not whole view controller) and it may be created and loaded again (in viewDidLoad, of course) in the future.

share|improve this answer
Thank you. This now makes sense. What I was missing is that the setter created by @synthesize will release the object before it sets the pointer to nil. I wondered why I wasn't seeing memory links in the profiler. Thank you for this answer! –  lp1756 Apr 16 '12 at 19:33

viewDidUnload is not called everytime before a dealloc, see what the apple docs say..

When a low-memory condition occurs and the current view controller’s views are not needed, the system may opt to remove those views from memory. This method is called after the view controller’s view has been released and is your chance to perform any final cleanup. If your view controller stores separate references to the view or its subviews, you should use this method to release those references. You can also use this method to remove references to any objects that you created to support the view but that are no longer needed now that the view is gone. You should not use this method to release user data or any other information that cannot be easily recreated.

so the idea behind it is too release any unwanted objects which can be created easily. Now coming to the part where it sets the properties to nil.. it does so because this way you release all the memory and set the objects to nil (thus bringing your down your memory usage) and after this if a dealloc is called your app will not crash as in objective-c you can send release messages to nil objects..

I would advise you to read the apple ViewController Programming Guide , it will clear a lot of your questions.... hoping this clears some of the air.. :D

share|improve this answer

Your Answer


