fuzzygoat
fuzzygoat

Reputation: 26223

iPhone, memory / autorelease objects

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

Upvotes: 1

Views: 122

Answers (2)

jlehr
jlehr

Reputation: 15617

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.

Upvotes: 2

Giao
Giao

Reputation: 15146

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.

Upvotes: 0

Related Questions