views:

25

answers:

1

Hey guys, sorry for beating the memory management (dead)horse again. I know this question has been asked several times on SO, but I feel all the threads out there is still lacking two pieces of information. Let me put forth what I DO know to save everyone some time.

1) When you create an IBOutlet, your view controller automatically retains this outlet.

1a) When you don't create an outlet, the 'framework'(the nib?) releases your objects(like uilabels or uiviews) for you.

2) When you do self.myOutlet = nil, you effectively release an outlet(providing that you have synthesized your properties correctly). This is because the setter releases the outlet and assigns it to nil.

What I don't know:

1) The MAIN question: If you do self.myOutlet = nil in viewDidUnLoad, do you still need to do anything in dealloc? Can you make the assumption that viewDidUnload always called before dealloc?(and therefore your retained views are released?)

2) If you don't synthesize a property for that outlet, what happens? Shouldn't the framework release it automatically?(since you don't have a retain property) If you do have to release it, how do you do it and where(in viewDidUnload or dealloc)?

If anything is wrong, please point it out to me. Any clarifications at all would be very helpful.

+1  A: 

(#1) The Apple docs say to do both

In addition, because of a detail of the implementation of dealloc in UIViewController, you should also set outlet variables to nil in dealloc:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmNibObjects.html

(#2) If you don't synthesize a property, you still need to make a property, and it better retain -- read the link. The UI object is created, autoreleased and the outlet property is set (which is supposed to retain). There is no release called for you because they already did the release they were supposed to do -- you retained, so you have to release (in both viewDidUnload and dealloc)

Lou Franco
hi lou, thanks for your response:
Ying
hi lou, thanks for your response: I am confused because that quote about setting outlet variables to nil in dealloc is in this section that says:"Note: On iOS prior to 3.0...you should set outlets to nil in setView:, as illustrated in this example:" Then it says, like you quoted above, "In addition...." So is this not part of the pre iOS 3.0 detail?So my take away from this is, when you have a retained property, during viewDidUnload AND dealloc, you have to do self.outlet = nil, and nothing else to manage property correctly. Do not have outlets without having properties. Is this right?
Ying
My reading of that note is that only the setView part applies to iOS 3.0 because viewDidUnload did not exist. The second part is for every version. Yes, use @property for IBOutlets.
Lou Franco