I has a bug where my application crashed "EXC_BAD_ACCESS" when I hit the back key on my navigation bar and the view unloaded that had a MapKit (mapView) and used the Location Manager. Tried for days to fix the bug and finally came up with a fix for anyone that comes across this problem:
Add this code to your dealloc
- (void)deall开发者_运维知识库oc {
mapView.delegate = nil;
locationManager.delegate = nil;
[mapView release];
[locationManager release];
}
I had this one too, :) And, yes, this fix is actually a proper fix;
- (void)dealloc {
mapView.delegate = nil;
locationManager.delegate = nil;
[mapView release];
[locationManager release];
}
What happens behind the scenes is this:
You hit the backkey. This unloads and in consequence releases the controller which holds the mapView. As there has been quite likely only a single reference to the controller it will be dealloc'ed then.
The locationManager, however, is quite likely still referenced somewhere in the inner workings of geopositioning.
If the locationManager and/or mapView now send out a notification to their respective delegate, they are following an invalid pointer. Which will result in a EXC_BAD_ACCESS exception.
Yes: nilling delegates that point to self is always a good idea. I justed wished Apple would add some automagic there.
精彩评论