For the following code, 1 – is retain needed on the object returned back from getFoo; 2 – is the release needed on foo in method func?
- (NSString *)getFoo {
return [[[NSString alloc] initWithString:@"foo"] autorelease];
}
- (void)func {
// ??? is the retain needed?
NSString *foo = [[self getFoo] retain];
// use foo
// ??? is the release needed?
[foo release];
}
Every
retainmust be match with arelease.This said, in your func you don’t need to
retain*foo if you are not delaying it’s use.Usually autorelease pool get’s
drainat the end of the run loop so you have the time to use it locally in your function.But if you
retainyou mustrelease.And you could do this :
This is a convenience methode that return an
autoreleaseobject to you.