Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-03-2008, 10:22 AM   #1 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default Retaining IBOutlets

Historically speaking, when working with IBOutlets on the Mac, you didn't retain the outlets in your controller class. There's an exception to this in that the root objects in the nib (the ones with an icon in the nib's main window) have to be released since they get created with a retain count of 1, but that is handled for us by the generic controller classes like WindowController, so in most situations, releasing IBOutlets simply wasn't necessary.

However, with the iPhone sample code, I've noticed that they've started declaring properties for the IBOutlets and also releasing the IBOutlets in the Dealloc. I wanted to ask about this change on Cocoa-dev, but the newer Cocoa sample code uses garbage collection, meaning it's only relevant to the iPhone SDK, so I'm sure I'd of gotten slapped down for asking about NDA stuff.

Anyway, here's my guess about what's happening, but haven't been able to confirm this in the documentation:

If you create a property or manually create KVC-compliant accessors and mutators, Interface Builder will use those mutator methods to assign objects to your IBOutlet. If you do not, then it will assign them directly to the instance variable. In the former case, if the mutator sends the object a retain or if you specified the optional retain attribute to our property (Apple does in their sample code), it is then necessary to release the object in your dealloc method.

Does this sound logical or right? Has anybody found a way to confirm or deny this information in the official documentation?

Thanks,
Jeff
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 12-23-2008, 12:05 PM   #2 (permalink)
Airsource Ltd
 
Join Date: Nov 2008
Location: Cambridge, UK
Posts: 3
Default Need to release if no property set.

Quote:
Originally Posted by jeff_lamarche View Post
Historically speaking, when working with IBOutlets on the Mac, you didn't retain the outlets in your controller class. There's an exception to this in that the root objects in the nib (the ones with an icon in the nib's main window) have to be released since they get created with a retain count of 1, but that is handled for us by the generic controller classes like WindowController, so in most situations, releasing IBOutlets simply wasn't necessary.

However, with the iPhone sample code, I've noticed that they've started declaring properties for the IBOutlets and also releasing the IBOutlets in the Dealloc. I wanted to ask about this change on Cocoa-dev, but the newer Cocoa sample code uses garbage collection, meaning it's only relevant to the iPhone SDK, so I'm sure I'd of gotten slapped down for asking about NDA stuff.

Anyway, here's my guess about what's happening, but haven't been able to confirm this in the documentation:

If you create a property or manually create KVC-compliant accessors and mutators, Interface Builder will use those mutator methods to assign objects to your IBOutlet. If you do not, then it will assign them directly to the instance variable. In the former case, if the mutator sends the object a retain or if you specified the optional retain attribute to our property (Apple does in their sample code), it is then necessary to release the object in your dealloc method.

Does this sound logical or right? Has anybody found a way to confirm or deny this information in the official documentation?

Thanks,
Jeff
Nearly - the direct assignation case does an retain as well. See
The Airsource - Memory Management and NIBS which has references to the relevant parts of the documentation.
airsource is offline   Reply With Quote
Old 12-23-2008, 01:06 PM   #3 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
Default

Jeff,

Outlets are retained by default. If you have retain properties for outlets then they are retained and must be released. If you don't have any properties for outlets then they are retained and must be released. If you have assign properties for outlets then they are not retained.

There is a secondary issue related to memory warnings that makes this all more complicated. In short outlets should be released when a memory warning is received, but this is somewhat complicated due to deficiencies in the UIViewController implementation.

See this thread:

Cocoabuilder - Outlets / IBOutlet declarations (was Re: Interface Builder & Wiring Objects)
PhoneyDeveloper is offline   Reply With Quote
Old 08-25-2009, 10:23 AM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 1
Default

Sorry to reopen this thread after such a long time, but I've been searching for a simple answer to this question and this was one of the posts that came up in Google.

Essentially if you use interface builder to connect an IBOutlet instance variable to a control, you must always release said IBOutlet in your own -dealloc method.

e.g. in your .h file.

@interface MyView : UIViewController {
IBOutlet UILabel *myLabel;
}
@end

.m file..

-(void) dealloc {
[super dealloc];

// release all IBOutlets..
[myLabel release];
}

You always need to do this, even if you have defined a retain property for the IBOutlet. The reason for this is that loadFromNib will call setValue:withKey for each IBOutlet on your view controller class, which uses the property's setter method or retains the object by default if no setter method is available. There's no system to automatically release things created by the NIB so you need to manually release them yourself.

[Reference: ]Resource Programming Guide: Nib Files. Also see lecture 11 of CS193P at Stanford on iTunes U if it's still available - about 57 minutes in.

Regards,
Ben
BenClayton is offline   Reply With Quote
Old 02-19-2010, 05:00 AM   #5 (permalink)
iPhone Developer, PlayDom
 
AmanApps's Avatar
 
Join Date: Jul 2009
Posts: 61
Default

If you don't release it on dealloc it will raise the memory footprint.

See more detail here with instrument ObjectAlloc graph.
AmanApps is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,861
Threads: 49,770
Posts: 213,057
Top Poster: BrianSlick (3,139)
Welcome to our newest member, melodizzzy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:00 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0