Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

OnMapReady invoked twice after orientation changes #9136

Closed

Description

@tobrun

Capturing from #9128 (comment): orientation changes can trigger a double OnMapReady invocation:

I've done some logging and that's what i got:

Map: new nativeMapView = 917213383
Map: onStart()
Map: onResume()
Map: getMapAsync callback
Map: onPause()
Map: onStop()
Map: onDestory()
Map: NativeMapView.destroy(), nativeMapView = 917213383
Map: new nativeMapView = 892499314
Map: onStart()
Map: onResume()
Map: onPause()
Map: onStop()
Map: onDestory()
Map: NativeMapView.destroy(), nativeMapView = 892499314
Map: new nativeMapView = 422282973
Map: onStart()
Map: onResume()
Map: getMapAsync callback
Map: onPause()
Map: onStop()
Map: onDestory()
Map: NativeMapView.destroy(), nativeMapView = 422282973
Map: new nativeMapView = 173994215
Map: onStart()
Map: onResume()
Map: getMapAsync callback
Map: getMapAsync callback
Map: onPause()
Map: onStop()
Map: onDestory()
Map: NativeMapView.destroy(), nativeMapView = 173994215
Map: new nativeMapView = 62699327
Map: onStart()
Map: onResume()
Map: loadIconForMarkerView - nativeMapView = 892499314

Those numbers are hashcodes of nativeMapViews.

cc @LukasPaczos

Activity

  1. added
    Android
    on May 30, 2017
  2. LukasPaczos commented on May 31, 2017

    @LukasPaczos
    Contributor

    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_FOLLOW CameraPosition 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).

  3. LukasPaczos commented on Jun 5, 2017

    @LukasPaczos
    Contributor

    UPDATE
    This can lead to exceptions being thrown:

    • 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)

  4. tobrun commented on Jun 6, 2017

    @tobrun
    MemberAuthor

    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?

  5. LukasPaczos commented on Jun 6, 2017

    @LukasPaczos
    Contributor

    @tobrun I was able to reproduce it on

    • Sony Xperia M4 Aqua E2303 (Android 5.0)

    • Nexus 5 (Android 6.0.1)

  6. LukasPaczos commented on Jun 7, 2017

    @LukasPaczos
    Contributor

    @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.

  7. tobrun commented on Jul 10, 2017

    @tobrun
    MemberAuthor

    @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.

  8. added this to the android-v5.2.0 milestone on Sep 11, 2017
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

    Androidbug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions