I am looking at this code lifted straight from the MapViewController.m file in the CurrentAddress sample available on Apple's web site:
- (void)dealloc
{
[reverseGeocoder release];
[mapView release];
[getAddressButton release];
[super dealloc];
}
- (IBAction)reverseGeocodeCurrentLocation
{
self.reverseGeocoder =
[[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
reverseGeocoder.delegate = self;
[reverseGeocoder start];
}
I am wondering what the function is of the autorelease when allocating the object. (The reverseGeocoder is an ivar in the MapViewController class set up with the retain property.) I have code similar to this in my application, and it seems to work either way.
Setting your
reverseGeocoderproperty increments the retain count (+1), but since you're creating the object withalloc+init(+1), you need toautorelease(-1) so that you do not end up with a 2 retain count.It does work either way, the only difference is that when you do not
autorelease, you leak.It sure is, but note that when you're using the
self.reverseGeocoderform, you're not accessing the ivar directly - instead, you're calling the relevantsetReverseGeocoder:function, that is either written by yourself or @synthesized by the compiler.See: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
And: What equivalent code is synthesized for a declared property?