Skip to content

MapboxMap leaking #389

Closed
Closed

Description

@tonnyavery

Having just this map in my widget

class StaticMapWidget extends HookWidget {
  final Spot spot;

  StaticMapWidget(this.spot);

 @override
  Widget build(BuildContext context) {
    final Completer<MapboxMapController> _controller = useMemoized(() => Completer());
    return Container(
      height: 300,
      child: MapboxMap(
          initialCameraPosition: CameraPosition(
              target: LatLng(spot.location.latitude, spot.location.longitude), zoom: 14),
          styleString: MapboxStyles.MAPBOX_STREETS
          ),
    );
  }
}

If I open my screen with map 3 times and go back, I still get 1 instance of my widget kept in memory. There is also 1 instance of MapboxMap sitting in memory.

Having my widget implemented with extra map callbacks:

class StaticMapWidget extends HookWidget {
  final Spot spot;

  StaticMapWidget(this.spot);

  @override
  Widget build(BuildContext context) {
    final Completer<MapboxMapController> _controller = useMemoized(() => Completer());

    final onMapCreated = useMemoized(
        () => (controller) {
              if (!_controller.isCompleted) _controller.complete(controller);
            },
        [spot]);

    final onStyleLoaded = useMemoized(
        () => () async {
              final MapboxMapController map = await _controller.future;
              await map.clearSymbols();
              vm.getMarkers().forEach((element) async {
                await map.addSymbol(element);
              });
            },
        [spot]);

    return Container(
      height: 300,
      child: MapboxMap(
          initialCameraPosition: CameraPosition(
              target: LatLng(spot.location.latitude, spot.location.longitude), zoom: 14),
          styleString: MapboxStyles.MAPBOX_STREETS,
          onMapCreated: onMapCreated,
          onStyleLoadedCallback: onStyleLoaded
          ),
    );
  }
}

Running the screen with widget 3 times and going back, I still see 3 instance of my widget and 3 instance of MapboxMap in memory.

Activity

  1. tonnyavery commented on Aug 21, 2020

    @tonnyavery
    ContributorAuthor

    I have been debugging this leak for the last few days and I am 100% sure none of my code is causing the leak. I also made sure that Spot that I pass inside doesn't cause the leak and even tested HookWidget itself.
    The only variable left is MapboxMap.

  2. tonnyavery commented on Aug 22, 2020

    @tonnyavery
    ContributorAuthor

    Just to add some more details. Because leak is there on both iOS and Android emulators, I suspect that Flutter implementation is causing the leak, not the native view.

  3. tonnyavery commented on Aug 23, 2020

    @tonnyavery
    ContributorAuthor

    Also the case on latest flutter 1.20.2 version

  4. TimothySealy commented on Aug 24, 2020

    @TimothySealy
    Collaborator

    Hi @aaverin, have you looked at issue #226? On iOS it seems that the (native) MapBoxController is not cleaned up. PR #370 should fix this. Not sure if there is a similar issue for Android.

  5. tonnyavery commented on Aug 25, 2020

    @tonnyavery
    ContributorAuthor

    @TimothySealy I would need to test after the PR is merged, but I am quite confident that I had the leak present on both iOS and Android when testing

  6. tonnyavery commented on Sep 5, 2020

    @tonnyavery
    ContributorAuthor

    I think the leak is mostly related to iOS, I don't notice any more laggy behavior on Android. Will look into memory profile later to confirm, but we should focus on iOS because now map is almost unusable there.

  7. tonnyavery commented on Sep 9, 2020

    @tonnyavery
    ContributorAuthor

    @m0nac0 Can I help in any way to solve this and other iOS performance issues? I can't release the iOS version of the project because of that.

  8. tonnyavery commented on Nov 6, 2020

    @tonnyavery
    ContributorAuthor

    I will close this one for now, map is much more performant on iOS device and issues with iOS emulator should be resolved by #413

  9. tonnyavery commented on Oct 4, 2021

    @tonnyavery
    ContributorAuthor

    I am reopening this one because I get a consistent leak with multiple instances of MapboxMap and MapboxMapController even if I don't attach any callbacks to the widget and do nothing.
    Reproducible on Android and iOS both.

  10. Layoneth commented on Jun 16, 2023

    @Layoneth

    Some solution here?

  11. tonnyavery commented on Jun 16, 2023

    @tonnyavery
    ContributorAuthor

    @Layoneth i don't get any leaks anymore in the app, having multiple different places where I show the map.

    If you still experience problems best would be to debug on your usage case, find the cause and propose a fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions