react-native-maps: Android Crashes java.lang.IndexOutOfBoundsException: Index: 235, Size: 225

Summary

Hey, on the initial mount, android crashes. It starts in the 1.21.0 version and it is still present in the 1.23.7 version.

I get this error:

Image

Reproducible sample code

<MapView
        ref={map}
        superClusterRef={superCluster}
        initialRegion={
          location.latitude && location.longitude
            ? {
                latitude: location.latitude,
                longitude: location.longitude,
                latitudeDelta: DEFAULT_LAT_DELTA,
                longitudeDelta: DEFAULT_LNG_DELTA,
              }
            : appConfig.MAP.INITIAL_REGION
        }
        toolbarEnabled={false}
        animationEnabled={false}
        showsUserLocation={!!permissions.locationGranted}
        showsMyLocationButton={false}
        style={styles.map}
        radius={40}
        onPress={onMapPress}
        onPanDrag={onMapDrag}
        renderCluster={renderCluster}
        googleRenderer="LATEST"
        {...otherProps}
      >
        {markers}
      </MapView>


interface Props {
  coordinate: { latitude: number; longitude: number };
  onMarkerPress?: (markerRef: MapMarker | null) => void;
}

const Marker = ({ coordinate, onMarkerPress }: Props) => {
  const ref = useRef<MapMarker>(null);

  return (
    <DefaultMarker
      ref={ref}
      centerOffset={{ x: 0.0, y: -26 }}
      coordinate={coordinate}
      tracksViewChanges={false}
      image={{ uri: appConfig.MAP.MARKER_IMAGE }}
      style={styles.marker}
      pinColor={COLORS['solid/blue']}
      zIndex={1}
      onPress={() => {
        if (onMarkerPress) {
          onMarkerPress(ref.current);
        }
      }}
    ></DefaultMarker>
  );
};

export default memo(Marker);

const styles = StyleSheet.create({
  marker: {
    width: 43,
    height: 52,
  },
});

Steps to reproduce

I also use “react-native-map-clustering” version 3.4.2.

Expected result

App not crashing

Actual result

App crashes

React Native Maps Version

1.23.7

What platforms are you seeing the problem on?

Android

React Native Version

0.76.9

What version of Expo are you using?

SDK 52

Device(s)

Honor, Android 9

Additional information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 20

Most upvoted comments

@ammar-madni the map already delayes display of Markers until map is ready

https://github.com/react-native-maps/react-native-maps/blob/master/src/MapView.tsx

so it doesn’t make too much sense to me

@salah-ghanim Any movement on this?

https://github.com/ammar-madni/repro-index-out-of-bounds

The repro above is pretty simple, when you first load the app the marker data is generated. The second time will be from the cache and thats what crashes the app with the index out of bounds exception.

Let me know if there is anything else I can do to help make this issue easier to debug and resolve.

@ammar-madni crash is still happening but error changed. The app crashes when the screen that contains the map is unmounted

05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <RCTView id=2024 parentTag=2028 isRoot=false />
05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <CellContainer id=2028 parentTag=618 isRoot=false />
05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <RCTText id=2032 parentTag=2118 isRoot=false />
05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <RCTText id=2036 parentTag=2118 isRoot=false />
05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <RNSVGCircle id=2038 parentTag=2040 isRoot=false />
05-21 18:44:58.822 15202 15202 E unknown:SurfaceMountingManager: <RNSVGGroup id=2040 parentTag=null isRoot=false />
05-21 18:44:58.823 15202 15202 E unknown:SurfaceMountingManager: <RNSVGSvgViewAndroid id=2042 parentTag=2118 isRoot=false />
05-21 18:44:58.823 15202 15202 E unknown:SurfaceMountingManager: <RCTText id=2046 parentTag=2118 isRoot=false />
05-21 18:44:58.823 15202 15202 W unknown:BridgelessReact: ReactHost{0}.handleHostException(message = "Index: 277, Size: 0")
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: Exception in native call
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: java.lang.IndexOutOfBoundsException: Index: 277, Size: 0
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at java.util.ArrayList.get(ArrayList.java:437)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.rnmaps.maps.MapView.getFeatureAt(MapView.java:1158)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.rnmaps.fabric.MapViewManager.getChildAt(MapViewManager.java:78)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.rnmaps.fabric.MapViewManager.getChildAt(MapViewManager.java:43)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.mounting.SurfaceMountingManager.removeViewAt(SurfaceMountingManager.java:503)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.mounting.mountitems.IntBufferBatchMountItem.execute(IntBufferBatchMountItem.java:121)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.mounting.MountItemDispatcher.executeOrEnqueue(MountItemDispatcher.java:387)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.mounting.MountItemDispatcher.dispatchMountItems(MountItemDispatcher.java:293)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.mounting.MountItemDispatcher.tryDispatchMountItems(MountItemDispatcher.java:126)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.FabricUIManager$3.runGuarded(FabricUIManager.java:837)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.bridge.GuardedRunnable.run(GuardedRunnable.java:29)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.fabric.FabricUIManager.scheduleMountItem(FabricUIManager.java:841)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.swmansion.worklets.AndroidUIScheduler.triggerUI(Native Method)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.swmansion.worklets.AndroidUIScheduler.lambda$new$0(AndroidUIScheduler.java:28)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.swmansion.worklets.AndroidUIScheduler.$r8$lambda$Owjjhl033zNSHsU6ZfWQNbZEGbI(Unknown Source:0)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.swmansion.worklets.AndroidUIScheduler$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.swmansion.worklets.AndroidUIScheduler$1.runGuarded(AndroidUIScheduler.java:49)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.facebook.react.bridge.GuardedRunnable.run(GuardedRunnable.java:29)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at android.os.Handler.handleCallback(Handler.java:938)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at android.os.Looper.loop(Looper.java:236)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at android.app.ActivityThread.main(ActivityThread.java:7861)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at java.lang.reflect.Method.invoke(Native Method)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:600)
05-21 18:44:58.823 15202 15202 E unknown:ReactNative: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)

@scottaglia Can you try clamping the index in the getFeatureAt method as below:

    public View getFeatureAt(int index) {
        // Clamp index to valid bounds to avoid IndexOutOfBoundsException.
        if (index < 0 || index >= features.size()) {
            return null;
        }
        return features.get(index);
    }