Which one of these is better practice?开发者_运维问答
A) retain and release the object myself later
NSMutableArray* array = [[self getArray] retain];
....
[array release];
B) autorelease from the function returning the object
getArray {
NSMutableArray* returnedArray = [[[NSMutableArray alloc] init] autorelease];
.....
return returnedArray;
}
The simplest rule of thumb when it comes to memory management in Objective-C is that you should release anything that you've explicitly allocated (alloc), copied (copy), newed up (new), or retained (retain).
The release should be done within the scope of the aforementioned actions. If you allocate space for an object that is returned by a method, you should autorelease it before returning it. So, given the two options you've provided, B is the recommended practice.
You could read and follow Apples guidelines on memory management and performance.
Personally I think the reasons for choosing one way over the other:
Using Autorelease pros:
- You can't stuff it up, memory will be freed at some point. That I like to think of as "falling into the pit of success".
cons:
- Using autorelease a lot may cause you memory problems as lots of objects build up awaiting be released by the autorelease pools.
Using retain/release pros:
- More control when your memory is used/freed.
- On ios apple recommends that you use release instead of autorelease whenever possible to keep the size of the pool small.
cons:
- Like C/C++ malloc/free new/delete you have to be careful to keep them matched up and it is easy to stuff that up, causing memory leaks.
- For member variables you have no choice, retain/release is it.
I think, whichever style you choose comes down to the situation your code is in and choosing the best style based on there pro's and con's. I don't think there is any one answer to this.
If you want to return an object you have to use the second approach. In all cases where possible you should use the retain-release approach because this uses less memory.
- If you
new
,alloc init
,retain
, orcopy
(NARC) an object, you have to release it. - When the name of a method starts with any of those words, it means it's being created for the caller, who has the responsibility to
release
the object when he is done with it.
Otherwise the method returned is not owned by the caller, and he has to indicate he wants to keep it callingretain
on the object. - If you can choose, don't abuse autorelease when memory is a concern.
Some comments:
- The first rule logically results in the second. Example: if the outcome of a function (the returned object) survives the execution of the function, all the function can do is autorelease the object.
- Unless there is a memory/performance concern, the overhead of automatic memory management is better than the memory leak chances plus the increased developing time.
- Try to retain/release symmetrically. Example: if you retained on init, then release on dealloc. Same for viewDidLoad/viewDidUnload.
- If you use @property(retain) you have to release in dealloc.
Example:
// created with autoreleased, just call retain if you intend to keep it
NSString *orange = [NSString stringWithString:@"orange"];
// created for the caller, you'll have to release it when you are done
NSObject *apple = [NSString initWithString:@"apple"];
精彩评论