mapbox-maps-android icon indicating copy to clipboard operation
mapbox-maps-android copied to clipboard

Mapbox SDK memory leak detected!

Open abdullahjhussein opened this issue 2 years ago • 1 comments

Environment

  • Android OS version: V12
  • Devices affected: Google Pixel 6
  • Maps SDK Version: V10.7.0

Mapbox SDK memory leak detected! MapboxMap object (accessing getStyle) should not be stored and used after MapView is destroyed.

abdullahjhussein avatar Aug 20 '22 15:08 abdullahjhussein

I also have this issue.

stefanmuljadi avatar Aug 25 '22 09:08 stefanmuljadi

I have this issue when popping back to the map fragment using androids navigation components. I believe it may be due to the new embedded mapView lifecycle components (v10.0.0-rc.5 onwards) destroying the map after navigation.

caustin13 avatar Sep 01 '22 01:09 caustin13

As a temporary fix, you can exclude the 'maps-lifecycle' plugin from the com.mapbox.maps dependency (Documentation states that this plugin was added into the com.mapbox.maps dependency from v10.0.0-rc.5). While this does remove the automatic lifecycle handling from the application - it allows for navigation back to a map fragment.

Tested and working with com.mapbox.maps:android:10.8.0-rc.1

caustin13 avatar Sep 07 '22 06:09 caustin13

Hey all!

This error log is printed when Style or MapboxMap object is stored as local variable and gets accessed after style was reloaded or map view was destroyed. It's not necessarily the leak itself, more of a warning that outdated object is used. In order to understand what's happening please provide more information / provide sample code reproducing it.

@caustin13 - about your use-case with fragments and navigation components - we do track that internally and plan to fix it in next 2 weeks.

kiryldz avatar Sep 07 '22 09:09 kiryldz

Hello there,

In my case , This Memory leak because i call pointAnnotationManager.onDestroy() in onDestroy.

the solution, in v10 the mapbox handle removing annotations without call pointAnnotationManager.onDestroy()

https://docs.mapbox.com/android/maps/guides/migrate-to-v10/#annotations

In pre-v10, AnnotationManager needed to be recycled in the onDestroy method of the host activity. In v10, there is no need to do it manually because AnnotationManager will be cleaned automatically along with the host, AnnotationPlugin.

abdullahjhussein avatar Sep 08 '22 06:09 abdullahjhussein

I also have this issue. Any solution?

swathysreenivas avatar Sep 28 '22 12:09 swathysreenivas

This issue appears to me when LocationPuck is enabled, and I navigate away from the screen.

E/Mapbox: [maps-android\Mbgl-Style]: Mapbox SDK memory leak detected! Style object (accessing setStyleLayerProperty) should not be stored and used after MapView is destroyed or new style has been loaded.
E/Mapbox: [maps-android\Mbgl-Style]: Mapbox SDK memory leak detected! Style object (accessing styleLayerExists) should not be stored and used after MapView is destroyed or new style has been loaded.

I receive this error in an infinite loop on my run logs.

Dependencies: implementation 'com.google.android.gms:play-services-location:20.0.0' implementation 'com.mapbox.maps:android:10.8.1'

  • using Jetpack Compose approach with NavHost and NavController
  • notice play-services-location:21.0.0' will crash the application altogether. Though probably a separate issue, using 20.0.0 for now. https://github.com/mapbox/mapbox-maps-android/issues/1715#issuecomment-1281086663

IsaacTrevino avatar Oct 18 '22 14:10 IsaacTrevino

@swathysreenivas @IsaacTrevino as @kiryldz had mentioned - the message you see in logs means you use Style object after MapView is destroyed, so please check if this is the case, e.g. lifecycle and when Map is destroyed.

notice play-services-location:21.0.0' will crash the application altogether. Though probably a separate issue, using 20.0.0 for now.

This is known issue - Google play services v21 have breaking change and are not compatible with the existing Maps SDK, more info https://github.com/mapbox/mapbox-maps-android/issues/1752. Please keep using v20 for now.

yunikkk avatar Oct 18 '22 14:10 yunikkk

@yunikkk @kiryldz thank you for responding I dont see how I am calling a Style object after the MapView is destroyed since Mapbox handles the LocationPuck rendering. All I do is enable it with the location plugin location.enable and upon switching between mapView and another page, it breaks.

Though it may not break right away it does after you switch repeatedly. Could this be a bug within the location plugin lifecycle not being synced with MapView?

IsaacTrevino avatar Oct 18 '22 19:10 IsaacTrevino

I can confirm a similar issue with the mapbox location plugin. I store no references to the style and have narrowed the problem down to simply enabling the location plugin and using the LocationPuck. Without it, no warning is ever raised.

Setting enabled to false will hide these warnings, but will also obviously not show any puck (which is undesired). This code is enough to trigger these warnings when switching between pages:

 mapView.location.updateSettings {
            enabled = true
}

shilangyu avatar Oct 24 '22 13:10 shilangyu

If anyone wants to try removing the plugin like @caustin13 suggested, here's how

implementation('com.mapbox.maps:android:10.8.1'){
    exclude group: 'com.mapbox.plugin', module: 'maps-lifecycle'
}

VahanLab avatar Nov 10 '22 08:11 VahanLab