You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
OnMapReady invoked twice after orientation changes #9136
I found out that this leads to some kind of memory leak.
I noticed that after rotating screen rapidly for a bit when using TRACKING_FOLLOWCameraPosition returns zoom == -1 and that is, because location source (custom in this case) provides data to MyLocationView hooked with MapboxMap which should be dead and is not being rendered (probably?).
Here are some logs:
11:02:10.209 Map: onStart()
11:02:10.277 Map: onResume()
11:02:10.419 Map: onMapReady - 254321204
11:02:10.742 Map: CameraPositionController - zoom is 5.0: 254321204
11:02:10.747 Map: CameraPositionController - zoom is 5.0: 254321204
11:02:10.756 Map: CameraPositionController - zoom is 5.0: 254321204
11:02:10.759 Map: CameraPositionController - zoom is 5.0: 254321204
11:02:10.761 Map: CameraPositionController - zoom is 5.0: 254321204
11:02:11.218 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:12.583 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:13.023 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:13.249 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:15.173 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:15.752 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:17.212 Map: CameraPositionController - zoom is 14.050000190734863: 254321204
11:02:17.493 Map: onPause()
11:02:17.573 Map: onStop()
11:02:17.603 Map: mapbox - onDestroyView()
11:02:17.629 Map: onDestroy()
11:02:18.102 Map: onStart()
11:02:18.156 Map: onResume()
11:02:18.277 Map: onPause()
11:02:18.344 Map: onStop()
11:02:18.354 Map: mapbox - onDestroyView()
11:02:18.362 Map: onDestroy()
11:02:18.586 Map: onStart()
11:02:18.648 Map: onResume()
11:02:18.696 Map: onMapReady - 47018988
11:02:18.804 Map: onMapReady - 53041228
11:02:18.862 Map: CameraPositionController - zoom is -1: 47018988
11:02:18.863 Map: CameraPositionController - zoom is -1: 47018988
11:02:18.866 Map: CameraPositionController - zoom is -1: 47018988
11:02:18.867 Map: CameraPositionController - zoom is -1: 47018988
11:02:18.867 Map: CameraPositionController - zoom is -1: 47018988
11:02:19.058 Map: CameraPositionController - zoom is 2.0: 53041228
11:02:19.064 Map: CameraPositionController - zoom is 2.0: 53041228
11:02:19.066 Map: CameraPositionController - zoom is 2.0: 53041228
11:02:19.069 Map: CameraPositionController - zoom is 2.0: 53041228
11:02:19.073 Map: CameraPositionController - zoom is 2.0: 53041228
11:02:19.124 Map: CameraPositionController - zoom is 14.050000190734863: 53041228
11:02:19.351 Map: CameraPositionController - zoom is -1: 47018988
11:02:20.629 Map: CameraPositionController - zoom is 14.050000190734863: 53041228
11:02:20.778 Map: onPause()
11:02:20.881 Map: onStop()
11:02:20.888 Map: mapbox - onDestroyView()
11:02:20.906 Map: onDestroy()
11:02:21.303 Map: onStart()
11:02:21.418 Map: onResume()
11:02:21.619 Map: onMapReady - 179363955
11:02:21.830 Map: CameraPositionController - zoom is -1: 47018988
11:02:21.890 Map: CameraPositionController - zoom is 5.0: 179363955
11:02:21.914 Map: CameraPositionController - zoom is 5.0: 179363955
11:02:21.920 Map: CameraPositionController - zoom is 5.0: 179363955
11:02:21.924 Map: CameraPositionController - zoom is 5.0: 179363955
11:02:21.926 Map: CameraPositionController - zoom is 5.0: 179363955
11:02:22.024 Map: CameraPositionController - zoom is -1: 47018988
11:02:22.355 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:22.389 Map: CameraPositionController - zoom is -1: 47018988
11:02:23.617 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:23.946 Map: CameraPositionController - zoom is -1: 47018988
11:02:24.542 Map: CameraPositionController - zoom is -1: 47018988
11:02:24.544 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:25.625 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:25.795 Map: CameraPositionController - zoom is -1: 47018988
11:02:26.655 Map: CameraPositionController - zoom is -1: 47018988
11:02:26.655 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:27.602 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:27.645 Map: CameraPositionController - zoom is -1: 47018988
11:02:28.675 Map: CameraPositionController - zoom is -1: 47018988
11:02:28.676 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:29.712 Map: CameraPositionController - zoom is -1: 47018988
11:02:29.712 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:30.726 Map: CameraPositionController - zoom is -1: 47018988
11:02:30.727 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:31.746 Map: CameraPositionController - zoom is -1: 47018988
11:02:31.746 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:32.817 Map: CameraPositionController - zoom is -1: 47018988
11:02:32.817 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:33.874 Map: CameraPositionController - zoom is -1: 47018988
11:02:33.875 Map: CameraPositionController - zoom is 14.050000190734863: 179363955
11:02:34.941 Map: CameraPositionController - zoom is -1: 47018988
hashcodes of MapboxMap instances
As you can see when callback fires twice in one cycle reference of MapboxMap is still alive, location services are still working, but it's not rendered and returns zoom == -1 with every location tick (therefore camera change).
when "leaked" MapboxMap tries to add polylines, ArrayIndexOutOfBoundsException is thrown
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.mapbox.mapboxsdk.maps.AnnotationManager.addPolylines(AnnotationManager.java:541)
at com.mapbox.mapboxsdk.maps.MapboxMap.addPolylines(MapboxMap.java:1275)
in AnnotationManager#addPolylines()
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addPolylines(polylines);
}
long id = 0;
Polyline p;
for (int i = 0; i < polylines.size(); i++) {
p = polylines.get(i);
p.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i]; //exception is thrown here
} else {
// unit test
id++;
}
p.setId(id);
annotations.put(id, p);
}
because NativeMapView#addPolylines() will return empty array (because of NativeMapView#isDestroyedOn() being true)
when "leaked" MapboxMap recreates TRACKING_FOLLOW mode (therefore tries to ease camera) NullPointerException is thrown
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.mapbox.mapboxsdk.geometry.LatLng.equals(java.lang.Object)' on a null object reference
at com.mapbox.mapboxsdk.maps.TrackingSettings.resetTrackingModesIfRequired(TrackingSettings.java:290)
at com.mapbox.mapboxsdk.maps.Transform.easeCamera(Transform.java:113)
at com.mapbox.mapboxsdk.maps.MapboxMap$3.run(MapboxMap.java:804)
because mapView.getCameraPosition() in Transform#invalidateCameraPosition() returns CameraPosition with LatLng == null (because of NativeMapView#isDestroyedOn() being true)
My guess is that posting the invocation of OnMapReady to the end of the message queue is resulting in this behaviour. If you destroy the previous map instance while this update it being posted. I haven't been able to reproduce the double invocation. @LukasPaczos do you have some more information on used Android OS or device?
@tobrun I removed posting onMapReady() callback to the end of message queue in MapView#onMapChanged() and it helped! Can this produce any other issues, though?
UPDATE onMapReady() is not invoked multiple times anymore, but exceptions stated above are still being thrown.
UPDATE V2
Actually, I can't reproduce latter exception anymore. Former is still present.
@LukasPaczos sorry for not following up sooner. The reason for adding that post was to make sure the view was measured. I'm now reworking initialisation code to wait for the surface to be created before creating the map object (we used to create this when the Mapview was created). This should remove the necessity of posting and resolve this issue.
Activity
LukasPaczos commentedon May 31, 2017
I found out that this leads to some kind of memory leak.
I noticed that after rotating screen rapidly for a bit when using
TRACKING_FOLLOWCameraPositionreturnszoom == -1and that is, because location source (custom in this case) provides data toMyLocationViewhooked withMapboxMapwhich should be dead and is not being rendered (probably?).Here are some logs:
As you can see when callback fires twice in one cycle reference of
MapboxMapis still alive, location services are still working, but it's not rendered and returnszoom == -1with every location tick (therefore camera change).LukasPaczos commentedon Jun 5, 2017
UPDATE
This can lead to exceptions being thrown:
MapboxMaptries to add polylines,ArrayIndexOutOfBoundsExceptionis thrownin
AnnotationManager#addPolylines()because
NativeMapView#addPolylines()will return empty array (because ofNativeMapView#isDestroyedOn()being true)MapboxMaprecreatesTRACKING_FOLLOWmode (therefore tries to ease camera)NullPointerExceptionis thrownbecause
mapView.getCameraPosition()inTransform#invalidateCameraPosition()returnsCameraPositionwithLatLng == null(because ofNativeMapView#isDestroyedOn()being true)tobrun commentedon Jun 6, 2017
My guess is that posting the invocation of OnMapReady to the end of the message queue is resulting in this behaviour. If you destroy the previous map instance while this update it being posted. I haven't been able to reproduce the double invocation. @LukasPaczos do you have some more information on used Android OS or device?
LukasPaczos commentedon Jun 6, 2017
@tobrun I was able to reproduce it on
Sony Xperia M4 Aqua E2303 (Android 5.0)
Nexus 5 (Android 6.0.1)
LukasPaczos commentedon Jun 7, 2017
@tobrun I removed posting
onMapReady()callback to the end of message queue inMapView#onMapChanged()and it helped! Can this produce any other issues, though?UPDATE
onMapReady()is not invoked multiple times anymore, but exceptions stated above are still being thrown.UPDATE V2
Actually, I can't reproduce latter exception anymore. Former is still present.
tobrun commentedon Jul 10, 2017
@LukasPaczos sorry for not following up sooner. The reason for adding that
postwas to make sure the view was measured. I'm now reworking initialisation code to wait for the surface to be created before creating the map object (we used to create this when the Mapview was created). This should remove the necessity of posting and resolve this issue.