Fix retain cycle causing crash in RouteOverlayController on NavigationMapView reuse #3222
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a crash due to a retain cycle caused as part of
NavigationMapView
reuse across multiple instances ofNavigationViewController
which was made available with the injection work in #3186.Implementation
RouteOverlayController.navigationViewDidLoad
defines two closures for handling map events. Each closure captures a strong reference toself
which will result in a retain cycle if the referencedNavigationMapView
object is used across multiple instances ofNavigationViewController
. The crash occurs on presentation of the subsequentNavigationViewController
Steps to reproduce this bug are:
NavigationMapView
NavigationViewController
and inject the map view created in step 1.NavigationViewController
ensuring the map view is still retained.NavigationViewController
and inject the map view created in step 1.RouteOverlayController.showRouteIfNeeded
whenRouteOverlayController.navigationViewData
(which is implicitly unwrapped) is accessed. The crashing controller instance will be the one created byNavigationViewController
in step 2.Using
weak self
references breaks the retain cycle allowingRouteOverlayController
to deinit when the owningNavigationViewController
is deinit.It's unclear to me at this point if the handlers added to
navigationMapView.mapView.mapboxMap
also need to be cleaned, but in the interim I believe this fix should address the more immediate crash.