Skip to content

Commit

Permalink
Fixing memory leaks (#706)
Browse files Browse the repository at this point in the history
* Fix optionals in location requests

* Make sure onStyleLoaded callback is invoked when map is loaded and ready

* Update iOS SDK

* Clear and dispose everything that could be holding references

* Clear more of the platform argument callbacks

* Clear method channel handler

* Dispose leaking MapboxGLPlatform

* Merge in #704 simplifying platform usage
Dispose all potential leaks: platform channel, platform callbacks, controller and any callbacks that could be set from outside

* Bring back final modifiers to variables

Co-authored-by: stanlry <[email protected]>
Co-authored-by: Tobrun <[email protected]>
  • Loading branch information
3 people authored Nov 13, 2021
1 parent 3b46c2c commit 3c252ba
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ios/mapbox_gl.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A new Flutter plugin.
s.public_header_files = 'Classes/**/*.h'
s.dependency 'Flutter'
s.dependency 'MapboxAnnotationExtension', '~> 0.0.1-beta.1'
s.dependency 'Mapbox-iOS-SDK', '~> 6.3.0'
s.dependency 'Mapbox-iOS-SDK', '~> 6.4.0'
s.swift_version = '4.2'
s.ios.deployment_target = '9.0'
end
Expand Down
7 changes: 6 additions & 1 deletion lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class MapboxMapController extends ChangeNotifier {
}

final OnStyleLoadedCallback? onStyleLoadedCallback;

final OnMapClickCallback? onMapClick;
final OnMapLongClickCallback? onMapLongClick;

Expand Down Expand Up @@ -977,4 +976,10 @@ class MapboxMapController extends ChangeNotifier {
Future<double> getMetersPerPixelAtLatitude(double latitude) async {
return _mapboxGlPlatform.getMetersPerPixelAtLatitude(latitude);
}

@override
void dispose() {
super.dispose();
_mapboxGlPlatform.dispose();
}
}
9 changes: 9 additions & 0 deletions lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ class _MapboxMapState extends State<MapboxMap> {
_mapboxMapOptions = _MapboxMapOptions.fromWidget(widget);
}

@override
void dispose() async {
super.dispose();
if (_controller.isCompleted) {
final controller = await _controller.future;
controller.dispose();
}
}

@override
void didUpdateWidget(MapboxMap oldWidget) {
super.didUpdateWidget(oldWidget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ abstract class MapboxGlPlatform {
throw UnimplementedError(
'getMetersPerPixelAtLatitude() has not been implemented.');
}

Future<void> addGeoJsonSource(
String sourceId, Map<String, dynamic> geojson) async {
throw UnimplementedError('addGeoJsonSource() has not been implemented.');
Expand Down Expand Up @@ -314,4 +314,8 @@ abstract class MapboxGlPlatform {
{String? belowLayerId}) async {
throw UnimplementedError('addFillLayer() has not been implemented.');
}
}

void dispose() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,10 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
.map((key, value) => MapEntry<String, String>(key, jsonEncode(value)))
});
}
}

@override
void dispose() {
super.dispose();
_channel.setMethodCallHandler(null);
}
}

0 comments on commit 3c252ba

Please sign in to comment.