CLLocationManager, MKReverseGeocoder, cleaning up memory
In the More iPhone 3 development book, after the author is done with the locationManager delegate method of getting updates, he puts this at the end of that method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// some code here
manager.delegate = nil;
[manager stopUpdatingLocation];
[manager autorelease];
}开发者_JAVA技巧
And similarly, in the MKReverseGeocoder delegate methods, when he's done he does this:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
//some code here
geocoder.delegate = nil;
[geocoder autorelease];
}
Why do you need to do that in order to clean up memory? I thought the rule was if you alloc/init it, you need to release it. Why does he add the locationManager and geocoder to the autorelease pool? Thanks.
This is done to avoid calling the delegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
or
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
when you dealloc the object that responds to those CLLocationManager/MKReverseGeocoder
protocols
精彩评论