views:

47

answers:

2

I am a little concerned about building up a large amount of autoreleased objects on the iPhone. My application is only simple so it should not be an issue, but I just wanted to check that methods (like below) are correct and acceptable

-(NSNumber *)numberFromCore {
    NSNumber *removedNumber = [[dataCore objectAtIndex:0] retain];
    [dataCore removeObjectAtIndex:0];
    return [removedNumber autorelease];
}

-(NSString *)coreSizeAsString {
    NSString *coreSize = [NSString stringWithFormat:@"%d", [dataCore count]];
    return coreSize;
}

Where possible I have used [[Class alloc] init] and [Class release], but should I also be looking to change convienience methods like those above.

gary

A: 

If you're concerned about the large amount of autoreleased objects, create an NSAutoReleasePool before you enter a loop. After the loop is completed, -drain the pool. That will minimize the amount of time autoreleased objects are kept around.

Giao
That would be -release on the iPhone, since it doesn't have GC.
jlehr
`-drain` plays better with GC environments; it's more intended to future proof the code. The documentation recommends using `-drain` instead of `-release`.
Giao
+2  A: 

Convenience methods like the ones you've shown should return objects that are not owned by the caller, which in the both of these cases means autoreleased objects, so your code is correct.

jlehr