1

Weird discovery, when I used a drag and drop to make a new IBOutlet, as shown below, not with a @property at all:

@interface SkinChoosingView : UIViewController {
    IBOutlet UIActivityIndicatorView * activityIndicator;
}

Xcode inserted a -release and set the outlet to nil in viewDidUnload. I looked in viewDidLoad though, and no -retain was there! This went against everything I know about memory management.

I figure apple must know a thing or two about this stuff though, so is there something behind the scenes happening here? I have never had leaks from these types of IBOutlets, and I've never released them.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
Alex Gosselin
  • 2,942
  • 21
  • 37

2 Answers2

3

Yes, it automatically retains the outlet for you when loading the NIB file unless you explicitly declare the property associated with the outlet as an assigned property.

And since it retains the outlet for you, you must release in viewDidUnload as the outlets will be reloaded by the time next viewDidLoad is called.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • But if I didn't declare a property at all, is it still retained? (aside from the one retain it gets by being a subview) I have typically used the declaration above to make weak references, for things I don't need a reference to after the view is gone, and I've never had leaks before. – Alex Gosselin Jun 27 '11 at 22:56
  • Yes, I think I just mentioned that. If you want to make a weak reference, you should declare it as an `assign` property. – Deepak Danduprolu Jun 27 '11 at 23:00
1

The answer is that it uses "Key-Value Coading", which means it calls -setValue:forKey:, which has a "Default Search Pattern". For ivars, it does something like [ivar autorelease]; ivar = [newvalue retain];.

The "current best practice" is to stick IBOutlet on properties instead of ivars (see here). This makes it obvious what memory management pattern is being used and is more resilient to typos (e.g. if you misspell the ivar).

Community
  • 1
  • 1
tc.
  • 33,468
  • 5
  • 78
  • 96
  • I guess that explains where the `retain` comes from. From a test I tried it actually retains three extra times, and releases two extra times with an outlet connected, give the net +1 that prevents it from deallocating. – Alex Gosselin Jun 28 '11 at 00:43