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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2009, 10:14 AM   #1 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default Memory Management in returning variables

I've been told that every time you allocate a variable, you have to release it. However, when I allocate a variable and then return it, there's no way to deallocate it. How do you release variables in this case or does the system auto-release them then?

Code:
(NSString *)sayHi:(NSString *)name {
  NSString *myName = [NSString alloc] initWithFormat:@"Hi, %@", name];
  
  // I can't release myName here, because then I wouldn't be able to return it.
  return myName;
}
Here's another example:

Code:
(NSString *)norrisizeMe:(NSString *)name {
  NSString *myName = [NSString alloc] initWithFormat:@"%@ Norris", name];

  // I can't release myName here, because I need it!
  return [NSString stringWithFormat:@"Hi, %@", myName;
}
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!

Last edited by chuck; 05-05-2009 at 10:24 AM.
chuck is offline   Reply With Quote
Old 05-05-2009, 11:02 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by chuck View Post
I've been told that every time you allocate a variable, you have to release it. However, when I allocate a variable and then return it, there's no way to deallocate it. How do you release variables in this case or does the system auto-release them then?

Code:
(NSString *)sayHi:(NSString *)name {
  NSString *myName = [NSString alloc] initWithFormat:@"Hi, %@", name];
  
  // I can't release myName here, because then I wouldn't be able to return it.
  return myName;
}
Here's another example:

Code:
(NSString *)norrisizeMe:(NSString *)name {
  NSString *myName = [NSString alloc] initWithFormat:@"%@ Norris", name];

  // I can't release myName here, because I need it!
  return [NSString stringWithFormat:@"Hi, %@", myName;
}
That's (one of the reasons) why Obj-C (NSObject, actually) provides an 'autorelease' mechanism.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 05-05-2009, 03:39 PM   #3 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default

Quote:
Originally Posted by Kalimba View Post
That's (one of the reasons) why Obj-C (NSObject, actually) provides an 'autorelease' mechanism.
So you're saying I just need to add:

Code:
[myName autorelease];
right before the return statement?
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 05-05-2009, 03:44 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by chuck View Post
So you're saying I just need to add:

Code:
[myName autorelease];
right before the return statement?
Yep. Or you use the abbreviated version, which combines the autorelease and return into one statement:
Code:
return [myName autorelease];
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, variables

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: 455
16 members and 439 guests
Domele, Duncan C, Feldspar, karatebasker, MacBook MH, Objective Zero, patapple, Paul Slocum, peterwilli, pipposanta, PixelInteractive, Punkjumper, rubyeim54, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

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