Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Return a NSMutableArray?

samurlesamurle Posts: 254Registered Users
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?



-(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];
}
Post edited by samurle on

Replies

  • BrianSlickBrianSlick Posts: 9,303Tutorial Authors, Registered Users
    It doesn't leak as you have it, but you aren't following convention. Just return an autoreleased array.
  • Duncan CDuncan C Posts: 8,024Tutorial Authors, Registered Users
    samurle;406461 said:
    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?



    -(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

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • samurlesamurle Posts: 254Registered Users
    Duncan C;406584 said:
    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:

    getArray


    with:

    newArray


    And the compiler warning message went away. I didn't know that was possible.
Sign In or Register to comment.