user377419
user377419

Reputation: 4999

Cocoa Touch - Deallocating IBOutlets

I used to declare IBOutlets for iPhone like so,

IBOutlet UILabel *myLabel

I just found out that the correct way to declare them is like,

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

But with the second and correct way do you have to release each IBOutlet in the -(void)dealloc method to avoid memory leaks?

Upvotes: 1

Views: 127

Answers (1)

Joost Schuur
Joost Schuur

Reputation: 4482

In short, they should be deallocated either way. In the 2nd approach, all you're doing is creating a getter/setter pair, which doesn't handle deallocation at the proper time. It has some logic that knows when to properly deallocate when an object is replaced, but it can't do the final cleanup there, because the setter still allocates a new object that way.

Look at it this way: You have to deallocate objects that you specifically allocated. If your outlet, through the course of your code, or by virtue of instantiated data from a NIB file was assigned an object, at some point you're responsible for cleaning up after it (unless it came from an autorelease convenience method).

Upvotes: 3

Related Questions