4

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?

lp1756
  • 846
  • 1
  • 9
  • 18
  • 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?](http://stackoverflow.com/questions/7318599/why-assign-nil-to-iboutlets-in-viewdidunload) – jscs Apr 16 '12 at 19:07

2 Answers2

3

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.

Community
  • 1
  • 1
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • 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
0

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

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115