Repository navigation
Top and bottom layout guide KVO causes crashes in some scenarios #9941
Description
Summary
Crashes have been reported that are related to KVO observation of the bounds property of topLayoutGuide and bottomLayoutGuide. So far, these crashes have only been observed when using a combination of Xcode 9 beta 6 (although the beta version of Xcode 9 may not matter) and testing on a device with iOS 10.x. The exact version of iOS may not matter although it does appear that the combination of Xcode 9 beta (iOS 11 SDK) and pre-iOS 11 on device may trigger this issue.
Details
The MGLMapView has several subviews that are drawn on top of the map (like a compass) that must be moved with respect to map view container's chrome. For example, if a navigation bar is initially hidden and then shown, the compass at the top of the map view should move down to stay under the bottom of the navigation bar and not be covered by it.
In #7716 we removed code that had been used until that point to solve for updating the map view subviews with a constraint system. The constraints were replaced with a manual view layout approach that is invoked when the containing view controller's top and bottom layout guides bounds property changes. This is done by observing the layout guides with KVO and invoking setNeedsLayout on MGLMapView when the bounds of the layout guides is observed to have changed. Setting needs layout results in a call to the map view's implementation of layoutSubviews that, in turn, calls a method that updates the location of the relevant map view subviews based on the map view's contentInset that is based on the latest top and bottom layout guides sizes.
Using the layout guides is a cleaner approach, especially since it allows us to avoid manipulating the map view's containing view controller's view's constraints.
We did fix a related crasher in #9109 that happened because it was now possible for layout guides that had previously been observed to be deallocated before observation could be removed..
The fix was to implement layout guide observation removal in -[MGLMapView willMoveToWindow:]. This appeared to have fixed the problem. However, using the combination of Xcode 9 beta and iOS 10.x, crashes like this have been reported again:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x17f37f00 of class _UILayoutGuide was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x1901d2b0> (
<NSKeyValueObservance 0x1901d280: Observer: 0x189b5600, Key path: bounds, Options: <New: NO, Old: NO, Prior: NO> Context: 0x23cfa10, Property: 0x1901c690>
)'
Note that, in iOS 11, the top and bottom layout guides have been deprecated. From Xcode 9b1 release notes:
Interface Builder uses UIView.safeAreaLayoutGuide as a replacement for the deprecated Top and Bottom layout guides in UIViewController. To use the new safe area, select Safe Area Layout Guides in the File inspector for the view controller, and then add constraints between your content and the new safe area anchors. This prevents your content from being obscured by top and bottom bars, and by the overscan region on tvOS. Constraints to the safe area are converted to Top and Bottom when deploying to earlier versions of iOS. (29323293)
A good writeup of the new Safe Area Layout Guide is available here.
I've also observed that willMoveToWindow is called multiple times depending on the view hierarchy the map view is embedded in. This is especially true if container views like a UIPageViewController is used. It does seem like it may be possible for a race condition to occur where a second or third call to willMoveToWindow unexpectedly re-reregisters observers on a view controller's layout guides just before that view controller and its guides are deallocated. This may be relevant to at least one related report that an observer was attempted to be removed even though it had not apparently been registered previously:
[MGLMapView removeLayoutGuideObserversIfNeeded]
*** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <0x106283e00> for the key path "bounds" from <_UILayoutSpacer 0x1c41d0680> because it is not registered as an observer.'
Actions
- We should determine if there are any work arounds we can use to make the current KVO based approach work
- If we can keep the KVO approach, we should also update it to conditionally work with the new Safe Area Layout with iOS 11+
- If we cannot guarantee the safety of the KVO approach, we should examine if (despite the drawback of manipulating things in the app space) it make sense to revert to something like the previous constraints based approach
- We should continue to try and repro and confirm if this issue really is specific to Xcode 9 / iOS 10.x (although I've been unsuccessful so far and I'm out of ideas to try and repro this crash)
Activity
boundsj commentedon Sep 13, 2017
It seems that the KVO approach is unsafe. The KVO + manual layout approach was cleaner in several ways but I think that the reintroduction of constraints in #9995 should work around the KVO related crashes we are seeing and is a reasonable approach given the limitations we are working with.
We should land #9995 before shipping v3.6.3 cc @fabian-guerra
boundsj commentedon Sep 14, 2017
This was fixed in #9995 that was merged into
release-ios-v3.6.0-android-v5.1.0and will be in our iOS v3.6.3 release.Curtis-Halbrook commentedon Sep 18, 2017
We just had one of our QA's reproduce the issue still, running with Mapbox 3.6.3 Including relevant part of the stack trace:
Looking at your repo, the tag
ios-3.6.3does have the changes to remove KVO, but as you can see above we're still seeing it. Is it possible that you published the wrong compiled Mapbox.framework?boundsj commentedon Sep 18, 2017
Not able to reproduce.
The Mapbox.framework/Info.plist in the downloaded Mapbox pod has the expected value for the MGLCommitHash key (9613582). Also, dwarfdump of the dSYM file from the v3.6.3 pod does not find the problematic method that was removed (
-[MGLMapView removeLayoutGuideObserversIfNeeded]) like it did for the dSYM in v3.6.2.This seems like it might be related to a CocoaPods cache issue or the Mapbox version not actually updating somehow.
Curtis-Halbrook commentedon Sep 19, 2017
I looked at the Info.plist inside the Mapbox.framework in both the built app I sideloaded onto the QA devices, and the one in the Pods folder of the app. The Pod's version said the bundle's version string was 3.6.3, but the build product was 3.6.2. Looks like I didn't do a "build clean" before creating the sideload.
Sorry for sending you on a wild goose chase. Or a snipe hunt. Or your metaphor of choice. Shaka, when the walls fell.
boundsj commentedon Sep 19, 2017
No worries @Curtis-Halbrook! Thanks for the update.