App Crash - [MKSpatialCollider insertAnnotationView:]

Hello,


In my application I have a map view with approx. 4000 annotations. It works fine displaying them on the map (with clusters on iOS 11), but once the user leaves the view controller the application cashes. As you can see from the trace below, MapKit tries to insert new annotations after the mapView as been deallocated.


Is this a bug with iOS 11? Or any ideas on how I could fix the problem?


Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000298


Crashed: com.apple.main-thread
0  MapKit                         0x193dc6bac -[MKSpatialCollider insertAnnotationView:] + 244
1  MapKit                         0x193d33334 -[MKNewAnnotationContainerView _updateClusterableAnnotationViews:withID:] + 428
2  CoreFoundation                 0x1837a0520 -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 232
3  MapKit                         0x193d33ed4 -[MKNewAnnotationContainerView updateAnnotationViewsForReason:] + 296
4  MapKit                         0x193c833d0 -[MKAnnotationContainerView deselectAnnotationView:animated:] + 236
5  MapKit                         0x193c9d248 -[MKMapView dealloc] + 464
6  CoreFoundation                 0x183859098 cow_cleanup + 112
7  CoreFoundation                 0x1837a0370 -[__NSArrayM dealloc] + 68
8  libobjc.A.dylib                0x182b3e138 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 836
9  UIKit                          0x18cd06c80 -[UIView dealloc] + 968
10 libobjc.A.dylib                0x182b3e138 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 836
11 CoreFoundation                 0x183797d30 _CFAutoreleasePoolPop + 28
12 CoreFoundation                 0x183877a94 __CFRunLoopRun + 2020
13 CoreFoundation                 0x183797fb8 CFRunLoopRunSpecific + 436
14 GraphicsServices               0x18562ff84 GSEventRunModal + 100
15 UIKit                          0x18cd6c2e8 UIApplicationMain + 208
16 APPNAME                  0x102087668 main (SBSearchResultsViewController.swift:20)
17 libdyld.dylib                  0x1832ba56c start + 4

Replies

I am experiencing the same issue and filed a bug at https://bugreport.apple.comrdar://35830744

File a bug with similar details and hopefully Apple will recognise that this is a problem

Thanks! I filed a bug as well - rdar://35836747

Is anyone still experiencing this issue, I am seeing this exact crash with the same stack trace on iOS 11.2

Yes, I have this bug too and filed a bug with Apple (see http://www.openradar.me/36318937).


MapKit            0x0000000192760134 -[MKSpatialCollider insertAnnotationView:] + 152
MapKit            0x00000001926cc1b8 -[MKNewAnnotationContainerView _updateClusterableAnnotationViews:withID:] + 456
MapKit            0x00000001926cc1b8 -[MKNewAnnotationContainerView _updateClusterableAnnotationViews:withID:] + 456
CoreFoundation    0x00000001821083c0 -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 232
MapKit            0x00000001926ccd58 -[MKNewAnnotationContainerView updateAnnotationViewsForReason:] + 296
MapKit            0x000000019261b9e8 -[MKAnnotationContainerView finishAddingAnnotationViews] + 32
MapKit            0x000000019265b684 -[MKMapView annotationManager:didAddAnnotationRepresentations:] + 60
MapKit            0x0000000192618660 -[MKAnnotationManager updateVisibleAnnotations] + 1492
MapKit            0x00000001926334d8 -[MKMapView _didChangeRegionMidstream:] + 244
MapKit            0x000000019264fe30 -[MKMapView mapLayer:didChangeRegionAnimated:] + 72
VectorKit         0x0000000192000a2c -[VKMapView map:didChangeRegionAnimated:] + 144
VectorKit         0x0000000102f6f324 0x102f64000 + 45860
VectorKit         0x0000000192068c8c __86-[VKMapCameraController _animateToPosition:pitch:yaw:duration:timingCurve:completion:]_block_invoke.264 + 76
VectorKit         0x000000019202d46c -[VKAnimation stopAnimation:] + 128
VectorKit         0x000000019202d7dc -[VKTimedAnimation stopAnimation:] + 52
VectorKit         0x000000019202d8ec -[VKTimedAnimation onTimerFired:] + 64
VectorKit         0x0000000191fb8f58 md::AnimationManager::onTimerFired+ 204632 (double) + 284
VectorKit         0x000000019220d328 md::MapEngine::layoutScene+ 2646824 (double, bool) + 92
VectorKit         0x000000019220d2b4 -[_MapEngineRenderQueueSource renderQueueForTimestamp:] + 36
VectorKit         0x00000001922c3bfc -[MDDisplayLayer drawToTexture:withTimestamp:completionHandler:prepareHandler:] + 148
VectorKit         0x000000019240eea4 -[MetalLayer onTimerFired:] + 112
VectorKit         0x000000019220fac4 md::MapEngine::renderScene(double, std::__1::function<void + 2656964 ()>) + 104
VectorKit         0x000000019220e980 md::MapEngine::onRenderTimerFired+ 2652544 (double) + 180
VectorKit         0x00000001924066e4 ggl::DisplayLink::onTimerFired+ 4716260 (double) + 40
VectorKit         0x00000001924065f4 -[_GGLDisplayLinkTarget displayLinkFired:] + 92
QuartzCore        0x0000000186167710 CA::Display::DisplayLink::dispatch_items+ 50960 (unsigned long long, unsigned long long, unsigned long long) + 672
IOKit             0x00000001824a5098 IODispatchCalloutFromCFMessage + 392
CoreFoundation    0x00000001821c8290 __CFMachPortPerform + 188
CoreFoundation    0x00000001821e3000 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
CoreFoundation    0x00000001821e2704 __CFRunLoopDoSource1 + 440
CoreFoundation    0x00000001821e01d8 __CFRunLoopRun + 2196
CoreFoundation    0x00000001820ffe58 CFRunLoopRunSpecific + 436
GraphicsServices  0x0000000183facf84 GSEventRunModal + 100
UIKit             0x000000018b77f67c UIApplicationMain + 236
Zoigl-Kalender    0x0000000100e1b460 main + 423008 (main.m:14)
libdyld.dylib     0x0000000181c1c56c start + 4

It ssems to work w/o crash, if showAnnotations:animated: is invoked with animated:NO

[_mapView showAnnotations:(NSArray *)annotations animated:NO]

Can you try to verify this?

Unfortunately, yes. iOS 11 has not been a good release for MapKit, it has a lot of annoying bugs. This bug tops the list of crashes in my app.

This issue still persists in iOS 11.2.2 (Working with Swift in Xcode 9.2)

Setting animated to "false" does not seem to help either. 😟

I ran into this error when attempting to remove an instance of MKClusterAnnotation from the map views annotations. Specifically when a annotation view is reused for an MKClusterAnnotation. Check to see if you do anything like:


[mapView removeAnnotations:mapView.annotations];


or if you're in Swift:


mapView.removeAnnotations(mapView.annotations)


or really any line of code where you might muck with an MKClusterAnnotation provided by MapKit. Oddly it seems like MKClusterAnnotation instances pile up in the `annotation` property of MKMapView and MapKit doesn't clean them all up right away.

I am experiencing exactly this. Hopefully Apple fixes this soon.


Has anyone found a workaround for now?

).Yes, it's true. The crashes also happen when animated is false, but they happen less often (at least in my case with about 80 annaotations I find it much more difficult to provoke the crash from the UI when animated is false

And Apple enginers are working on this bug report, they two times asked me for additional information. So there is hope that this will be resolved soon.

The workaround that worked for me was to deselect annotations (cluster ones are the problem) before adding or removing any other annotations. My scenario

I realize your case may not involve adding or removing annotations, but my app crashes in the same MKSpatialCollider code path, so thought I'd mention this workaround. You might try deselecting annotations before the view goes away.

As pointed out in the other replies, this is a MapKit-internal bug.


However, the following code fixes the crash for me (workaround until Apple has finally fixed it). I added it to the view controller class that shows the map view.


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    // We need to remove all annotations here in order to prevent a MapKit-internal crash:
    mapView.removeAnnotations(mapView.annotations)
}

In my case the crash occurred after selecting a clustered annotation and dismiss/pop viewcontroller.
Deselecting all annotations in deinit fixes the crash for me.


deinit {
        mapView.selectedAnnotations.forEach({ mapView.deselectAnnotation($0, animated: false) })
    }

Still has crash after add below code


if (self.mapView.annotations.count > 0) {

[self.mapView removeAnnotations:self.mapView.annotations];

}