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 04-06-2009, 06:30 PM   #1 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Question What to Release

In the following example, should you release versionCombined because that is the string that is initially allocated, or versionLabel since that's what the string is ultimately assigned to?

Code:
- (void) versionLabel {
	NSString *versionShort = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
	NSString *versionCombined = [[NSString alloc] initWithFormat:@"%@ (%@)", versionShort, version];
	versionLabel.text = versionCombined;
 }
Thanks for clarifying.
DenVog is offline   Reply With Quote
Old 04-06-2009, 06:57 PM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by DenVog View Post
In the following example, should you release versionCombined because that is the string that is initially allocated, or versionLabel since that's what the string is ultimately assigned to?

Code:
- (void) versionLabel {
	NSString *versionShort = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
	NSString *versionCombined = [[NSString alloc] initWithFormat:@"%@ (%@)", versionShort, version];
	versionLabel.text = versionCombined;
 }
Thanks for clarifying.
In this code, you are responsible only for "versionCombined", because you are alloc/init-ing it.
Kalimba is offline   Reply With Quote
Old 04-06-2009, 07:12 PM   #3 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Thanks for your reply

Quote:
Originally Posted by Kalimba View Post
In this code, you are responsible only for "versionCombined", because you are alloc/init-ing it.
That's what I originally thought, but if I try to release versionCombined, I get an "error: 'versionCombined' undeclared (first use in this function)". Whereas if I release versionLabel, it seems fine.
DenVog is offline   Reply With Quote
Old 04-06-2009, 07:17 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by DenVog View Post
Thanks for your reply

That's what I originally thought, but if I try to release versionCombined, I get an "error: 'versionCombined' undeclared (first use in this function)". Whereas if I release versionLabel, it seems fine.
If you're getting that error, it's during compilation, so you probably have a typo/misspelling between the variable you're declaring and the one you're releasing.
Kalimba is offline   Reply With Quote
Old 04-06-2009, 07:18 PM   #5 (permalink)
New Member
 
Join Date: Feb 2009
Location: Houston, TX
Posts: 2
Default

yes you are responsible for VersionCombined
chsculpt is offline   Reply With Quote
Old 04-06-2009, 07:27 PM   #6 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Quote:
Originally Posted by Kalimba View Post
If you're getting that error, it's during compilation, so you probably have a typo/misspelling between the variable you're declaring and the one you're releasing.
Hmmm. I am literally doing a copy of the text "versionCombined" from my versionLabel method to the - (void)dealloc method. So if I understand your suggestion, I'm not sure how I've gotten a typo.
DenVog is offline   Reply With Quote
Old 04-06-2009, 07:30 PM   #7 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by DenVog View Post
Hmmm. I am literally doing a copy of the text "versionCombined" from my versionLabel method to the - (void)dealloc method. So if I understand your suggestion, I'm not sure how I've gotten a typo.
OK, I see. The "versionCombined" variable is a local variable in the "versionLabel" method only. It's not a member variable, so it won't be available in your dealloc method. You should just release versionCombined once you've assigned its value to versionLabel.text in the versionLabel method.
Kalimba is offline   Reply With Quote
Old 04-06-2009, 07:40 PM   #8 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Smile

Quote:
Originally Posted by Kalimba View Post
OK, I see. The "versionCombined" variable is a local variable in the "versionLabel" method only. It's not a member variable, so it won't be available in your dealloc method. You should just release versionCombined once you've assigned its value to versionLabel.text in the versionLabel method.
Ah. OK. Think I'm with you now. So I should really release versionCombined from within my versionLabel method, and then release versionLabel in my dealloc method.

Code:
- (void) versionLabel {
	NSString *versionShort = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
	NSString *versionCombined = [[NSString alloc] initWithFormat:@"%@ (%@)", versionShort, version];
	versionLabel.text = versionCombined;
	[versionCombined release];
 }
Thanks for your help. So much to learn...
DenVog is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, allocation, memory, memory management, release

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: 285
18 members and 267 guests
2WeeksToGo, ADY, Creativ, dacapo, Dani77, Fritzer, ghost, HDshot, headkaze, iDifferent, mer10, mystic.purple, Rudy, smethorst, stoneage, superg, tathaastu, Zool
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,878
Threads: 89,224
Posts: 380,732
Top Poster: BrianSlick (7,129)
Welcome to our newest member, olga2000
Powered by vBadvanced CMPS v3.1.0

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