|
From the text:
"With this in mind, here's a guideline for the use of -autorelease:
* If you need to defer ownership of an object, autorelease it."
I would suggest that that be re-written as "If you need to defer relinquishment of ownership..." If you have retained an object, you already "own" it; you want to give up ownership at some later time.
In the next paragraph:
"To simplify that, if you want to create an object and give it to something else and then completely forget about it, make sure that it's autoreleased. Another good example is if you want to create an array without having to release every object you add to it after you're done; autorelease, and it's all done for you."
This is a misleading over-simplification, and contradicts your later (itself perhaps over-exaggerated) exhortation to avoid autorelease.
It is perfectly reasonable to simply release an object that has been put into an array. Contrast
aNumber = [NSNumber numberWithInt:n];
[aMutableArray addObject:aNumber];
with
aNumber = [[NSNumber alloc] initWithInt:n];
[aMutableArray addObject:aNumber];
[aNumber release];
The latter is both correct from the perspective of memory management and more efficient.
mmalc
|
For simple programs, autoreleasing in this sort of situation is not just convenient but probably preferable since you don't need the gain in efficiency.
I suppose I had better restate the ideal:
In programs where efficiency is key, avoid autoreleasing if possible. In programs where simplicity is key, autorelease where the convenience helps.
Your mileage may vary; strike your own balance.