reversegeocoder autorelease issue
I'm learning to use the mkreversegeocoder classes and have got it working using the following three lines of code and implementing the didFindPlacemark method.
geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]];
[geoCoder setDelegate:self];
[geoCoder start];
The above works fine, however... in trying to improve my app by thinking about managi开发者_StackOverflow社区ng memory and resources, i am trying to add an autorelease the geoCoder allocation as such:
geoCoder = [[[MKReverseGeocoder alloc] initWithCoordinate:[u coordinate]] autorelease];
The above pattern is used by apple in their documentation so it seems like the right thing to do. However, when I add the autorelease, the didFindPlacemark method never gets called. It's as if the autorelease releases the geoCoder object immediately.
The geoCoder object is declared as an ivar, so it should work. the apple example using this pattern works so the problem must be with my implementation, but i cannot work out where i am going wrong.
i would appreciate anyone input as to whats happening and how i can get this going.
best regards
Have you defined a retain property for geoCoder
like in the Apple sample app CurrentAddress?
In their sample app, the geocoder is set using the property accessor otherwise the autorelease will release the ivar when you exit the method.
Change that line to:
self.geoCoder = [[[MKReverseGeocoder alloc] initWith...] autorelease];
Also be sure to release it in dealloc
:
- (void)dealloc
{
[geoCoder release];
[super dealloc];
}
精彩评论