开发者

MapKit/Location Manager crashes app when unloading view

开发者 https://www.devze.com 2022-12-24 19:17 出处:网络
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

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:

  1. 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.

  2. The locationManager, however, is quite likely still referenced somewhere in the inner workings of geopositioning.

  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消