michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.28k stars 220 forks source link

fix: NPE when removing single location updates #248

Open ifarhanpatel opened 1 year ago

ifarhanpatel commented 1 year ago

Overview

When getting the location from playServices, the app crashes for some users. Not able to reproduce it. It has also been reported Here

Crash Log

Fatal Exception: java.lang.NullPointerException: Listener must not be null at com.google.android.gms.common.internal.Preconditions.checkNotNull(SourceFile:2) at com.google.android.gms.common.api.internal.ListenerHolders.createListenerKey(SourceFile:1) at com.google.android.gms.location.FusedLocationProviderClient.removeLocationUpdates(SourceFile:6) at o.computeScrollRange$4.onLocationResult(SourceFile:74) at com.google.android.gms.internal.location.zzaw.notifyListener(SourceFile:2) at com.google.android.gms.common.api.internal.ListenerHolder.zaa(SourceFile:2) at com.google.android.gms.common.api.internal.zacb.run(:4) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:257) at android.app.ActivityThread.main(ActivityThread.java:8183) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)

Test Plan

I'm assuming the fix is straightforward. We deployed a patch of this in our app with success.

MarkCSmith commented 1 year ago

I encountered this bug after upgrading from version 3.0.2 to version 3.0.6 of this package. One easy way to reproduce it is to have your app make a second call to getCurrentPosition() before the first call has finished. The mSingleLocationCallback member variable is overwritten when the second call is made (therefore losing track of the first LocationCallback). The mSingleLocationCallback member is set to null when one of the getCurrentPosition() calls finishes finding a location. Then, when the second "get position" call finishes finding a location, null is passed to mFusedLocationClient.removeLocationUpdates() and the Listener must not be null exception occurs.

A little re-architecture might be a good idea, e.g., maintain a list of pending getCurrentPosition() callbacks instead of making a new call to mFusedLocationClient.requestLocationUpdates() for each call an app makes.