<p>If I have something like a UILabel linked to a xib file, do I need to release it on dealloc of my view? The reason I ask is because I don't alloc it, which makes me think I don't need to release it either?
eg (in the header):</p>

<pre><code>IBOutlet UILabel *lblExample;
</code></pre>

<p>in the implementation:</p>

<pre><code>....
[lblExample setText:@"whatever"];
....

-(void)dealloc{
    [lblExample release];//?????????
}
</code></pre>

[{'answer_id': 61841, 'author': 'Sören Kuklau', 'author_id': 1600, 'author_profile': 'https://Stackoverflow.com/users/1600', 'pm_score': 0, 'selected': False, 'text': '<p>Related: <a href="https://stackoverflow.com/questions/6578/understanding-reference-counting-with-cocoa-objective-c">Understanding reference counting with Cocoa / Objective C</a></p>\n'}, {'answer_id': 61867, 'author': 'Kendall Helmstetter Gelner', 'author_id': 6330, 'author_profile': 'https://Stackoverflow.com/users/6330', 'pm_score': 0, 'selected': False, 'text': '<p>You do alloc the label, in a sense, by creating it in IB.</p>\n\n<p>What IB does, is look at your IBOutlets and how they are defined.  If you have a class variable that IB is to assign a reference to some object, IB will send a retain message to that object for you.</p>\n\n<p>If you are using properties, IB will make use of the property you have to set the value and not explicitly retain the value.  Thus you would normally mark IBOutlet properties as retain:</p>\n\n<pre><code>@property (nonatomic, retain) UILabel *lblExample;\n</code></pre>\n\n<p>Thus in ether case (using properties or not) you should call release in your dealloc.</p>\n'}, {'answer_id': 61942, 'author': 'rustyshelf', 'author_id': 6044, 'author_profile': 'https://Stackoverflow.com/users/6044', 'pm_score': 2, 'selected': False, 'text': '<p>I found what I was looking for in the Apple docs. In short you can set up your objects as properties that you release and retain (or just @property, @synthesize), but you don\'t have to for things like UILabels:</p>\n\n<p><a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/chapter_3_section_4.html#//apple_ref/doc/uid/10000051i-CH4-SW18" rel="nofollow noreferrer">http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/chapter_3_section_4.html#//apple_ref/doc/uid/10000051i-CH4-SW18</a></p>\n'}, {'answer_id': 62985, 'author': 'Eric Allam', 'author_id': 7434, 'author_profile': 'https://Stackoverflow.com/users/7434', 'pm_score': 0, 'selected': False, 'text': "<p>Any IBOutlet that is a subview of your Nib's main view does not need to be released, because they will be sent the autorelease message upon object creation.  The only IBOutlet's you need to release in your dealloc are top level objects like controllers or other NSObject's.  This is all mentioned in the Apple doc linked to above.</p>\n"}, {'answer_id': 191935, 'author': 'mmalc', 'author_id': 23233, 'author_profile': 'https://Stackoverflow.com/users/23233', 'pm_score': 6, 'selected': True, 'text': '<p>If you follow what is now considered to be best practice, you <em>should</em> release outlet properties, because you should have retained them in the set accessor:</p>\n\n<pre><code>@interface MyController : MySuperclass {\n    Control *uiElement;\n}\n@property (nonatomic, retain) IBOutlet Control *uiElement;\n@end\n\n\n@implementation MyController\n\n@synthesize uiElement;\n\n- (void)dealloc {\n    [uiElement release];\n    [super dealloc];\n}\n@end\n</code></pre>\n\n<p>The advantage of this approach is that it makes the memory management semantics explicit and clear, <em>and it works consistently across all platforms for all nib files</em>.</p>\n\n<p>Note: The following comments apply only to iOS prior to 3.0. With 3.0 and later, you should instead simply nil out property values in viewDidUnload.</p>\n\n<p>One consideration here, though, is when your controller might dispose of its user interface and reload it dynamically on demand (for example, if you have a view controller that loads a view from a nib file, but on request -- say under memory pressure -- releases it, with the expectation that it can be reloaded if the view is needed again).  In this situation, you want to make sure that when the main view is disposed of you also relinquish ownership of any other outlets so that they too can be deallocated. For UIViewController, you can deal with this issue by overriding <code>setView:</code> as follows:</p>\n\n<pre><code>- (void)setView:(UIView *)newView {\n    if (newView == nil) {\n        self.uiElement = nil;\n    }\n    [super setView:aView];\n}\n</code></pre>\n\n<p>Unfortunately this gives rise to a further issue. Because UIViewController currently implements its <code>dealloc</code> method using the <code>setView:</code> accessor method (rather than simply releasing the variable directly), <code>self.anOutlet = nil</code> will be called in <code>dealloc</code> as well as in response to a memory warning... This will lead to a crash in <code>dealloc</code>.</p>\n\n<p>The remedy is to ensure that outlet variables are also set to <code>nil</code> in <code>dealloc</code>:</p>\n\n<pre><code>- (void)dealloc {\n    // release outlets and set variables to nil\n    [anOutlet release], anOutlet = nil;\n    [super dealloc];\n}\n</code></pre>\n'}, {'answer_id': 567962, 'author': 'Wil Shipley', 'author_id': 30602, 'author_profile': 'https://Stackoverflow.com/users/30602', 'pm_score': 2, 'selected': False, 'text': "<p>The </p>\n\n<pre><code>[anOutlet release], anOutlet = nil;\n</code></pre>\n\n<p>Part is completely superfluous if you've written setView: correctly.</p>\n"}, {'answer_id': 2295783, 'author': 'Shaikh Sonny Aman', 'author_id': 133539, 'author_profile': 'https://Stackoverflow.com/users/133539', 'pm_score': 1, 'selected': False, 'text': '<p>If you don’t release it on dealloc it will raise the memory footprint. </p>\n\n<p><a href="http://amanpages.com/iphone-app-development-core-sdk-cocoa/iphone-iboutlet-should-be-released-manually/" rel="nofollow noreferrer">See more detail here with instrument ObjectAlloc graph</a></p>\n'}, {'answer_id': 10135225, 'author': 'stephen', 'author_id': 680161, 'author_profile': 'https://Stackoverflow.com/users/680161', 'pm_score': 0, 'selected': False, 'text': "<p>If you dont set the IBOutlet as a property but simply as a instance variable, you still must release it. This is because upon initWithNib, memory will be allocated for all IBOutlets. So this is one of the special cases you must release even though you haven't retained or alloc'd any memory in code.</p>\n"}]
['https://Stackoverflow.com/questions/61838', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6044/']