Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I crashing after MKMapView is freed if I'm no longer using it?

Tags:

I have a MKMapView. Sometimes after my view controller is dismissed, I'll get a EXC_BAD_ACCESS.

I turned on NSSZombies and it looks like the MKMapView's delegate — my view controller! — is being called, despite both the MKMapView and UIViewController subclass being freed. I've checked, and my memory management is correct.

What's going on?

like image 765
Steven Fisher Avatar asked Feb 02 '10 22:02

Steven Fisher


2 Answers

This is because of the way MKMapView works. There's an operation pending, so MapKit is retaining the MKMapView and it hasn't actually been deallocated yet. That isn't itself a problem. The problem is that it's still sending messages to your delegate.

The workaround is simple: As part of your view controller's cleanup set the map view's delegate to nil, which will prevent MKMapView from sending messages to it.

This is documented in MKMapViewDelegate Protocol Reference:

Before releasing an MKMapView object for which you have set a delegate, remember to set that object’s delegate property to nil. One place you can do this is in the dealloc method where you dispose of the map view.

Edit: Give Oscar an upvote as well, just below, who provided the documentation quote here.

Given ARC, I suggest this means you should set your map view's delegate to nil in your view controller's dealloc.

like image 100
Steven Fisher Avatar answered Oct 29 '22 21:10

Steven Fisher


OK, this is the confirmation of the answer. It's from the Apple doc, but it's missing from MKMapView. It's only found under the documentation for its delegate protocol:

Before releasing an MKMapView object for which you have set a delegate, remember to set that object’s delegate property to nil. One place you can do this is in the dealloc method where you dispose of the map view.

NOTE: This also applies to UIWebView.

I set the MapView's delegate pointer to nil in the delegate's dealloc method, and our crashes seem to have been eliminated.

like image 27
Oscar Avatar answered Oct 29 '22 21:10

Oscar