Skip to content

Commit e21265a

Browse files
authored
Fix possible memory leak while rerouting (#4380)
* Fix possible memory leak while rerouting
1 parent 8139d06 commit e21265a

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* Limited number of reported alternatives to 2, i.e. the `updatedAlternatives` parameter in `RouterDelegate.router(_:didUpdateAlternatives:removedAlternatives:)` has maximum size of 2. ([#4367](https://github.com/mapbox/mapbox-navigation-ios/pull/4367))
5252
* Increased terminal offboard route request timeout from 4 to 15 seconds. ([#4367](https://github.com/mapbox/mapbox-navigation-ios/pull/4367))
5353
* Sped up onboard routing cancellation. ([#4367](https://github.com/mapbox/mapbox-navigation-ios/pull/4367))
54+
* Fixed an issue when a rerouting could cause a memory leak. ([#4380](https://github.com/mapbox/mapbox-navigation-ios/pull/4380))
5455

5556
## v2.10.0
5657

Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ final class Navigator: CoreNavigator {
235235

236236
private func unsubscribeNavigator() {
237237
stopUpdatingElectronicHorizon()
238+
rerouteController.invalidate()
238239
if let navigatorStatusObserver = navigatorStatusObserver {
239240
navigator.removeObserver(for: navigatorStatusObserver)
240241
}

Sources/MapboxCoreNavigation/RerouteController.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ class RerouteController {
9797
self.navigator?.addRerouteObserver(for: self)
9898
}
9999

100+
func invalidate() {
101+
navigator?.removeRerouteObserver(for: self)
102+
navigator?.setRerouteControllerForController(defaultRerouteController.nativeInterface)
103+
}
104+
100105
deinit {
101-
self.navigator?.removeRerouteObserver(for: self)
102-
self.navigator?.setRerouteControllerForController(defaultRerouteController.nativeInterface)
106+
invalidate()
103107
}
104108
}
105109

@@ -231,7 +235,9 @@ extension RerouteController: RerouteControllerInterface {
231235
return
232236
}
233237

234-
reroutingRequest = customRoutingProvider.calculateRoutes(options: routeOptions) {result in
238+
reroutingRequest = customRoutingProvider.calculateRoutes(options: routeOptions) { [weak self] result in
239+
guard let self = self else { return }
240+
235241
switch result {
236242
case .failure(let error):
237243
callback(.init(error: RerouteError(message: error.localizedDescription,

Sources/TestHelper/NavNative/NativeNavigatorSpy.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class NativeNavigatorSpy: MapboxNavigationNative.Navigator {
1414
public var passedNavigationTrackerOptions: PredictiveLocationTrackerOptions?
1515
public var passedDescriptorsTrackerOptions: PredictiveLocationTrackerOptions?
1616
public var passedDatasetTrackerOptions: PredictiveLocationTrackerOptions?
17+
public var passedRemovedRerouteObserver: RerouteObserver?
1718

1819
public var rerouteController: RerouteControllerInterface!
1920
public var rerouteDetector: RerouteDetectorInterface!
@@ -54,6 +55,10 @@ public class NativeNavigatorSpy: MapboxNavigationNative.Navigator {
5455
callback(returnedChangeLegResult)
5556
}
5657

58+
public override func removeRerouteObserver(for observer: RerouteObserver) {
59+
passedRemovedRerouteObserver = observer
60+
}
61+
5762
@_implementationOnly public override func setRerouteControllerForController(_ controller: RerouteControllerInterface) {
5863
passedRerouteController = controller
5964
}

Tests/MapboxCoreNavigationTests/RerouteControllerTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ final class RerouteControllerTests: TestCase {
134134
wait(for: [expectation], timeout: 1)
135135
}
136136

137+
func testInvalidate() {
138+
rerouteController.invalidate()
139+
XCTAssertTrue(navigatorSpy.passedRerouteController === navigatorSpy.getRerouteController())
140+
XCTAssertTrue(navigatorSpy.passedRemovedRerouteObserver === rerouteController)
141+
}
142+
137143
}

0 commit comments

Comments
 (0)