Exception when zooming with overlays on MKMapView: NSInvalidArgumentException NSSetM removeObject: object cannot be nil
Asked Answered
L

1

8

I am adding around 1000 MKPolygons onto an MKMapView. After adding the overlays onto the MKMapView it works fine. However if I zoom in and zoom out quickly (occasionally letting go of the zoom so the mapView processes it's new visibleMapRect) I find the app (sometimes) crashes with the following exception stack trace:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSSetM removeObject:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000107db6b0b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010781b141 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000107d1712a -[__NSSetM removeObject:] + 538
    3   VectorKit                           0x000000010bae84fa -[VKRasterOverlayTileSource removeOverlay:] + 89
    4   VectorKit                           0x000000010b79a648 -[VKMapModel removeRasterOverlay:] + 68
    5   MapKit                              0x0000000106f93bf1 -[MKOverlayContainerView _removeDrawable:forOverlay:level:] + 502
    6   MapKit                              0x0000000106f92633 -[MKOverlayContainerView addAndRemoveOverlayViews] + 785
    7   MapKit                              0x0000000106f44772 -[MKMapView _didChangeRegionMidstream:] + 229
    8   MapKit                              0x0000000106f49528 -[MKMapView mapLayer:didChangeRegionAnimated:] + 91
    9   VectorKit                           0x000000010b7d39e8 -[VKMapCameraController rotateToYaw:withPoint:animated:] + 884
    10  VectorKit                           0x000000010b7d4e7b -[VKMapCameraController snapMapIfNecessary:] + 389
    11  MapKit                              0x0000000106f93bf1 -[MKOverlayContainerView _removeDrawable:forOverlay:level:] + 502
    12  MapKit                              0x0000000106f92633 -[MKOverlayContainerView addAndRemoveOverlayViews] + 785
    13  MapKit                              0x0000000106f44772 -[MKMapView _didChangeRegionMidstream:] + 229
    14  MapKit                              0x0000000106f49528 -[MKMapView mapLayer:didChangeRegionAnimated:] + 91
    15  VectorKit                           0x000000010b9f7b0e -[VKScreenCameraController stopPinchingWithFocusPoint:] + 64
    16  MapKit                              0x0000000106fbb1e3 __38-[MKMapGestureController handlePinch:]_block_invoke.184 + 126
    17  VectorKit                           0x000000010b78aaa2 -[VKAnimation stopAnimation:] + 109
    18  VectorKit                           0x000000010b8f9a3d -[VKDynamicAnimation stopAnimation:] + 45
    19  MapKit                              0x0000000106fb7819 -[MKMapGestureController stopDynamicAnimations] + 50
    20  MapKit                              0x0000000106fba254 -[MKMapGestureController gestureRecognizerTouchesBegan:] + 39
    21  MapKit                              0x0000000106fbbab1 -[_MKUserInteractionGestureRecognizer touchesBegan:withEvent:] + 198
    22  UIKit                               0x0000000108e9b934 -[UIGestureRecognizer _touchesBegan:withEvent:] + 113
    23  UIKit                               0x0000000108e8901c __55-[UIGestureEnvironment _updateGesturesForEvent:window:]_block_invoke + 337
    24  UIKit                               0x0000000108e89b79 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 282
    25  UIKit                               0x0000000108e88e0a -[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
    26  UIKit                               0x00000001089d4eea -[UIWindow sendEvent:] + 4092
    27  UIKit                               0x0000000108981a84 -[UIApplication sendEvent:] + 352
    28  UIKit                               0x00000001091655d4 __dispatchPreprocessedEventFromEventQueue + 2926
    29  UIKit                               0x000000010915d532 __handleEventQueue + 1122
    30  UIKit                               0x000000010915e800 __handleEventQueue + 5936
    31  CoreFoundation                      0x0000000107d5cc01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    32  CoreFoundation                      0x0000000107d420cf __CFRunLoopDoSources0 + 527
    33  CoreFoundation                      0x0000000107d415ff __CFRunLoopRun + 911
    34  CoreFoundation                      0x0000000107d41016 CFRunLoopRunSpecific + 406
    35  GraphicsServices                    0x000000010cc97a24 GSEventRunModal + 62
    36  UIKit                               0x0000000108964134 UIApplicationMain + 159
    37  MyProject                                0x0000000106e51a9f main + 111
    38  libdyld.dylib                       0x000000010e56165d start + 1

This doesn't always happen. Often it's quite rare. You can see that it's hard to debug and work out what's going wrong because my project doesn't appear in the stack trace (except that it's running in my application...).

When trying to track down the issue I created a very basic app with nothing but an MKMapView and 1000 random MKPolygons with 5 randomly generated CLLocationCoordinate2D each. I found that the app still crashes when zooming, resting, zooming, resting, etc. But less likely? All I am doing here is adding basic random MKPolygons to the MKMapView! Note the MKPolygonRenderer for each MKPolygon does nothing apart from give it a random fillColor.

Note: I am running the application in Xcode on an iPhone 7 Simulator.

What's going wrong here? Thanks!

Librarianship answered 17/8, 2017 at 10:13 Comment(6)
The message [__NSSetM removeObject:]: object cannot be nil is pretty clear. Find the location where an object is removed from an NSMutableSet and add a nil check.Sosna
reason: '*** -[__NSSetM removeObject:]: object cannot be nil' Here it's remove some object. Maybe your Annotation array give empty content like that?. Use Exception error handling it automatically shows your crash class and line.Thinnish
This NSMutableSet is nested deep into the MapKit framework somewhere, I can't modify that code. I also am not using any Annotations. Only MKPolygons all of which are non-nil.Librarianship
Did you found a solution? Have the same problem right now :(Sennacherib
@Skyborg, unfortunately no, perhaps it's a bug in the Apple maps framework which they will fix at some point? It is rare, I've stopped experiencing it.Librarianship
i moved to mapbox. the problem exist since more than 1,5 years and apple didn´t fixed it.Sennacherib
O
1

I had a very similar problem when adding & removing annotations to a map view.

The answer turned out to be put everything that accesses the annotations - even calculations that only read the annotations array - onto the main thread.

I had only put the actual adding & removing on the main thread and it was ok for the most part, but would randomly crash with

[__NSSetM removeObject:]: object cannot be nil

Ouch answered 5/7, 2018 at 17:7 Comment(1)
Thanks for your answer. I initially thought it could be a threading issue too, but I do everything in the example above, which is failing, on the main thread.Librarianship

© 2022 - 2024 — McMap. All rights reserved.