transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.66k stars 426 forks source link

com.transistorsoft.locationmanager.adapter.BackgroundGeolocation is leaking activity #2155

Closed ioanpalalogos closed 1 week ago

ioanpalalogos commented 1 month ago

Your Environment

Expected Behavior

The library should not leak. The reference to the activity should be released.

Actual Behavior

I'm running leak canary 2.14 to detect memory leaks in my app and it seems that the library is causing some memory leaks. In RNBackgroundGeolocationModule.java -> initializeLocationManager() method it's set a reference to the activity BackgroundGeolocation adapter = getAdapter(); adapter.setActivity(activity); but it is never released. Also, it seems that the adapter doesn't update the activity onHostResume if the module it's already initialized: https://github.com/transistorsoft/react-native-background-geolocation/blob/60a9ff6d6744f15b4ecc0f33689d57b8eda7d044/android/src/main/java/com/transistorsoft/rnbackgroundgeolocation/RNBackgroundGeolocationModule.java#L329

Steps to Reproduce

1. 2. 3. 4.

Context

Debug logs

Logs ``` ┬─── │ GC Root: System class │ ├─ java.lang.Thread class │ Leaking: NO (a class is never leaking) │ ↓ static Thread.defaultUncaughtExceptionHandler │ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ├─ com.transistorsoft.locationmanager.adapter.BackgroundGeolocation$w0 instance │ Leaking: UNKNOWN │ Retaining 16 B in 1 objects │ ↓ BackgroundGeolocation$w0.b │ ~ ├─ com.transistorsoft.locationmanager.adapter.BackgroundGeolocation instance │ Leaking: UNKNOWN │ Retaining 1.2 kB in 47 objects │ c instance of com.myapp.android.MainApplication │ d instance of com.myapp.android.ui.web2.WebViewActivity with mDestroyed = │ true │ ↓ BackgroundGeolocation.d │ ~ ╰→ com.myapp.android.ui.web2.WebViewActivity instance ​ Leaking: YES (ObjectWatcher was watching this because com.myapp.android. ​ ui.web2.WebViewActivity received Activity#onDestroy() callback and ​ Activity#mDestroyed is true) ​ Retaining 406.8 kB in 3480 objects ​ key = 0797987c-8709-435a-b6af-f2c5f02921f8 ​ watchDurationMillis = 5556 ​ retainedDurationMillis = 555 ​ mApplication instance of com.myapp.android.MainApplication ​ mBase instance of androidx.appcompat.view.ContextThemeWrapper METADATA Build.VERSION.SDK_INT: 35 Build.MANUFACTURER: Google LeakCanary version: 2.14 App process name: com.myapp.android Class count: 46964 Instance count: 404481 Primitive array count: 225380 Object array count: 47088 Thread count: 197 Heap total bytes: 54103532 Bitmap count: 15 Bitmap total bytes: 640429 Large bitmap count: 0 Large bitmap total bytes: 0 Db 1: open /data/user/0/com.myapp.android/databases/com.google.android. datatransport.events Db 2: open /data/user/0/com.myapp.android/no_backup/androidx.work.workdb Db 3: open /data/user/0/com.myapp.android/databases/google_analytics_v4.db Db 4: open /data/user/0/com.myapp.android/databases/transistor_logback.db Db 5: open /data/user/0/com.myapp.android/databases/transistor_location_manager Db 6: open /data/user/0/com.myapp.android/databases/store1.db Db 7: closed /data/user/0/com.myapp.android/databases/myapp.db Db 8: closed /data/user/0/com.myapp.android/databases/myapp.db Db 9: closed /data/user/0/com.myapp.android/databases/myapp.db Db 10: closed /data/user/0/com.myapp.android/databases/myapp.db Db 11: closed /data/user/0/com.myapp.android/databases/myapp.db Db 12: closed /data/user/0/com.myapp.android/databases/myapp.db Db 13: closed /data/user/0/com.myapp.android/databases/myapp.db Db 14: closed /data/user/0/com.myapp.android/databases/myapp.db Db 15: open /data/user/0/com.myapp.android/databases/myapp.db Stats: LruCache[maxSize=3000,hits=205035,misses=370405,hitRate=35%] RandomAccess[bytes=18512916,reads=370405,travel=277660589952,range=59389616,size =76615658] Analysis duration: 8787 ms ```
github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 week ago

This issue was closed because it has been inactive for 14 days since being marked as stale.