Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

objective c - Use autorelease when setting a retain property using dot syntax?

I see in some sample code that autorelease is used. I am not familiar with the instances when this is required. For example, if I create an annotation object

Header file

@interface someViewController: UIViewController 
{
    Annotation *annotation;
}

@property (nonatomic, retain) Annotation *annotation;

@end

Implementation file

@implementation someViewController
@synthesize annotation
@end

Question: Is it the correct approach if I initialize my annotation object in the implementation file like this?

self.annotation = [[Annotation alloc] initWithCoordinate:location];

Do I need to set autorelease for this? Or can I just do it the normal way and add the release in the dealloc method?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

this is correct:

self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];

because annotation property is declared as a retain property, so assigning to it will increment its retain count.

you will also need, all the same, to release self.annotation in -dealloc.

in short:

  1. init will set retain count to 1;

  2. assigning to self.annotation, will set it to 2;

  3. autorelease will set it back to 1 when the main loop is executed again;

  4. release in dealloc will set the retain count to 0, so that the object will be deallocated);

the best way to think of autorelease is the following, in my opinion: autorelease will "schedule" an "automatic" release for your object at some (near) point in future (typically when the control flow goes back to the main loop, but details are hidden in the hands of Apple).

autorelease is mostly useful in conjunction with init, specifically in the following cases:

  1. when you init a local variable, so that you don't have to release it explicitly before it goes out of scope (the main loop will do that for you);

  2. when you return a pointer to an object you have just created without keeping ownership of it (typical case of the create/make* kind of selectors, the receiver is required to retain it to get ownership);

  3. with properties that retain, when you assign to them an object that they should own uniquely;

  4. with data structures that increment the retain count (NSMutableArray, NSMutableDictionary, etc): you should generally autorelease a newly inited object when you add it to such data structure.

apart from case 2, it is evident that the use of autorelease is meant to improve readability of the code and reduce the potential for errors (meaning that in all of the other cases, you could simply release explicitly your object after the assignment or at the end of the scope).

when using properties, you have always to check whether they are of the retain or assign/copy case; in the first case, assigning a newly inited object to a property generally requires autorelease.

Anyway, I would suggest at least skimming one of the many tutorial on memory management for iOS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...