views:

28

answers:

1

Do I have to "release" my UI objects that I declared as IBOutlets with property attributes "retain" and "nonatomic"? I ask because I have a UI var declared as so...

@interface MyViewController : UIViewController 
{
IBOutlet UILabel *lblStatus;
}

@property (retain, nonatomic) IBOutlet UILabel *lblStatus;

@end

and my dealloc like so...

- (void)dealloc 
{
  //[lblStatus release];
  [super dealloc];
}

and with the lblStatus UI var commented out, Instruments doesn't seem to detect any leaks when I pop the view off the navigation stack.

Thanks in advance for your help!

A: 

Since they're retained, yes, you are responsible for releasing them. Usually, with view controllers, that should happen in -viewDidUnload, like so:

- (void)viewDidUnload
{
    self.lblStatus = nil;
    [super viewDidUnload];
}

(Setting the property's value, with a synthesized retain accessor, will release the old value before setting the instance variable to the new value.)

Noah Witherspoon
thanks, Noah! if i uncomment the "release" statement in my dealloc method, would that suffice as well? also, any thoughts on why Instruments isn't picking up the leak with the "release" statement commented out? thanks again.
BeachRunnerJoe
Not exactly. If your view gets unloaded and loaded again—for instance, if your view controller goes offscreen, gets a memory warning, and appears onscreen again later—then it will get reinstantiated, and any retained outlets (like your label) will probably leak.
Noah Witherspoon
@Noah, are you sure about that ?
Pierre Valade
@Pierre, not entirely; it's possible that the NIB-loading system is smart enough to detect that the outlets are already hooked up and not bother reinstantiating things. I wouldn't rely on that, though, and in my apps I assume each -loadView is effectively starting from scratch.
Noah Witherspoon