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

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

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 12-06-2009, 12:48 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 34
Default Specifc question to MemoryMgmt

I've a question about releasing instance variables in the init-method:

Code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier: reuseIdentifier]) {
			
			//set slider
			UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 20, 210, 20)];
			self.distanceSlider = slider;
			[slider release];
			[distanceSlider addTarget:self action:@selector(sliderDidChange:) forControlEvents:UIControlEventValueChanged];
			[[self contentView] addSubview:distanceSlider];
			
			//[distanceSlider release];
			...
    }
    return self;
}

...
- (void)dealloc {
	[distanceSlider release];
	[super dealloc];
}
My question is if I should release the instance variable "distanceSlider" in the init method (marked red)?
By adding the UISlider as a subview to the content view of my table cell the retain count of the instance variable is increased +1? So should I release distanceSlider after I added to the subview?
The Apple MemoryMgmt guide says that variables should only be released if they were created with a method's name that begins with “alloc” or “new” or contains “copy”.
zword is offline   Reply With Quote
Old 12-06-2009, 02:02 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

Assuming that distanceSlider is a @property, then no you should not release it there. It will be released in dealloc as you already have.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 12-08-2009, 06:32 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 34
Default

Quote:
Originally Posted by BrianSlick View Post
Assuming that distanceSlider is a @property, then no you should not release it there. It will be released in dealloc as you already have.
@BrianSlick:
You're right distanceSlider is a property.

I was asking if distanceSlider has to be released twice because I thought it was responsible for the retain count of the instance variable. And when I add the slider as subview to the contentView of my TableCell the retain count is increased +1.

But as I understand it now, each pointer is responsible for releasing itself from the pointed address. So in this case, distanceSlider releases its pointer in the dealloc method. And the TableCell in the class in which it will be used.
I hope I've explained it right.
zword is offline   Reply With Quote
Old 12-08-2009, 10:16 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

The +1 you saw was from addSubview. The parent view is retaining its subviews. You do not need to worry about that. If you later remove it from that view, or if it still exists when that view is deallocated, a release message will be automatically sent at that time.

Your responsibility is to balance your memory-related actions - alloc, copy, new, retain - with a release.

It is not your responsibility to guess how Apple's classes work. The implementation for addSubview could retain your slider one time, or it could retain it many times. You have no way of knowing. You have to trust that whatever Apple's classes DO, they will also UNDO appropriately. Same for arrays, dictionaries, etc. The act of adding an object to an array, just like adding your slider to a view, does not involve alloc, copy, or new (unless you do it wrong), so it should not be released.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 12-11-2009, 10:44 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 34
Default

Quote:
Originally Posted by BrianSlick View Post
The +1 you saw was from addSubview. The parent view is retaining its subviews. You do not need to worry about that. If you later remove it from that view, or if it still exists when that view is deallocated, a release message will be automatically sent at that time.

Your responsibility is to balance your memory-related actions - alloc, copy, new, retain - with a release.

It is not your responsibility to guess how Apple's classes work. The implementation for addSubview could retain your slider one time, or it could retain it many times. You have no way of knowing. You have to trust that whatever Apple's classes DO, they will also UNDO appropriately. Same for arrays, dictionaries, etc. The act of adding an object to an array, just like adding your slider to a view, does not involve alloc, copy, or new (unless you do it wrong), so it should not be released.
@BrianSlick: Thanks for the explanation.
zword is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, release, retain

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
» Online Users: 243
16 members and 227 guests
@sandris, ADY, Alsahir, dacapo, Dani77, djohnson, HemiMG, jansan, JasonR, MarkC, mer10, prchn4christ, ryandb2, smethorst, tomtom100
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:59 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0