it is right:
self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];
since the annotation property is declared as a hold property, therefore assigning it increases the save counter.
you will also need, however, to release self.annotation in -dealloc .
:
init will set the hold value to 1;
assigning self.annotation will set it to 2;
autorelease will return it to 1 when the main loop is executed again;
a release in dealloc will set the hold value to 0 so that the object is freed);
the best way to think about autorelease is this: in my opinion: autorelease will “plan” an “automatic” release for your object at some (nearest) point in the future (usually when the flow control returns to the main loop, but the details are hidden in the hands Apple).
autorelease is mainly useful in combination with init , in particular in the following cases:
when you init local variable, so you do not need to release explicitly before it leaves the scope (the main loop will do this for you);
when you return a pointer to the object that you just created without retaining ownership of it (a typical case of create/make* selectors, the recipient needs to retain it to obtain ownership);
with properties that retain when you assign them an object to which they must belong uniquely;
with data structures that increase the retention count ( NSMutableArray , NSMutableDictionary , etc.): usually you should autorelease add a new init ed object when adding it to such a data structure.
except in case 2, it is obvious that the use of autorelease intended to improve the readability of the code and reduce the likelihood of errors (which means that in all other cases you could just release explicitly specify the object after the assignment or at the end of the area).
when using properties, you should always check whether they relate to the case of retain or assign / copy ; in the first case, assigning an init ed object to a property usually requires autorelease .
In any case, I would suggest at least removing one of the many memory management guides for iOS .
source share