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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-02-2012, 10:43 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question Return a NSMutableArray?

I'm trying to return a NSMutableArray from a method.
I prefer not to use autorelease.

Is this a valid allocation and release, or will it leak?
Will the alloc go out of scope as soon as the getArray method ends?


Code:
-(NSMutableArray *)getArray {

 NSMutableArray *arr = [[NSMutableArray alloc] init];

 [arr addObject:@"Test1"];
 [arr addObject:@"Test2"];
 [arr addObject:@"Test3"];

 return arr;
}

-(void) myMethod {

 NSMutableArray *arr = [self getArray];

 for(NSString *str in arr) {
    NSLog(@"string: %@", str );
 }

 [arr release];
}
samurle is offline   Reply With Quote
Old 02-02-2012, 11:07 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

It doesn't leak as you have it, but you aren't following convention. Just return an autoreleased array.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 02-03-2012, 09:09 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by samurle View Post
I'm trying to return a NSMutableArray from a method.
I prefer not to use autorelease.

Is this a valid allocation and release, or will it leak?
Will the alloc go out of scope as soon as the getArray method ends?


Code:
-(NSMutableArray *)getArray {

 NSMutableArray *arr = [[NSMutableArray alloc] init];

 [arr addObject:@"Test1"];
 [arr addObject:@"Test2"];
 [arr addObject:@"Test3"];

 return arr;
}

-(void) myMethod {

 NSMutableArray *arr = [self getArray];

 for(NSString *str in arr) {
    NSLog(@"string: %@", str );
 }

 [arr release];
}
There is a strong convention in Cocoa programming that methods like this should return an autoreleased object, and the caller should retain it if they want to keep it.

If you want to return a retained object, have the name begin with "new".

In fact, the LLVM compiler uses this naming convention when it does code analysis. It expects methods to return autoreleased objects unless their names begins with new, alloc, copy, or mutableCopy. Methods who's names DO start with new, alloc, copy, or mutableCopy are expected to return retained object.

This is a really, really good idea to follow. When you follow that rule, it's obvious what you need to do with any given object.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 05:51 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 245
samurle is on a distinguished road
Question

Quote:
Originally Posted by Duncan C View Post
There is a strong convention in Cocoa programming that methods like this should return an autoreleased object, and the caller should retain it if they want to keep it.

If you want to return a retained object, have the name begin with "new".

In fact, the LLVM compiler uses this naming convention when it does code analysis. It expects methods to return autoreleased objects unless their names begins with new, alloc, copy, or mutableCopy. Methods who's names DO start with new, alloc, copy, or mutableCopy are expected to return retained object.

This is a really, really good idea to follow. When you follow that rule, it's obvious what you need to do with any given object.
Thanks, that's exactly what I want to do. Return a retained object, but not an autorelease object.

Hopefully, returning a retained object does not automatically make it an autorelease object
behind the scenes?

I replaced:
Code:
getArray
with:
Code:
newArray
And the compiler warning message went away. I didn't know that was possible.
samurle is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, array, nsmutablearray

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: 405
17 members and 388 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, QuantumDoja, reficul, Retouchable, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

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