Karumi / Dexter

Android library that simplifies the process of requesting permissions at runtime.
http://karumi.com
Apache License 2.0
5.23k stars 671 forks source link

Memory Leak #197

Open szhupeng opened 6 years ago

szhupeng commented 6 years ago

Expected behaviour

grant permission but no memory leak

Actual behaviour

cause memory leak by Dexter.instance hold the refrences of activity

Steps to reproduce

use LeakCanary to watch the activity that use dexter to grant permission

Version of the library

4.2.0

Serchinastico commented 6 years ago

Hey @szhupeng,

Can you reproduce it with the sample project?

autonomousapps commented 6 years ago

Having the same issue. Here's a sample stack trace from leak canary:

04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: In com.chess:3.6.6-SNAPSHOT:19666.
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * com.chess.ui.activities.MainActivity has leaked:
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * GC ROOT static com.karumi.dexter.Dexter.instance
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * references com.karumi.dexter.DexterInstance.listener
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * references com.karumi.dexter.MultiplePermissionListenerThreadDecorator.listener
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * references com.chess.ui.fragments.settings.SettingsThemeFragment$1.val$parent (anonymous implementation of com.karumi.dexter.listener.multi.MultiplePermissionsListener)
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * references com.chess.ui.views.FListView.mContext
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * leaks com.chess.ui.activities.MainActivity instance
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Retaining: 312 KB.
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Reference Key: 1a39194e-5414-46bb-9b6c-d172eb987af6
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Device: Genymotion Android Google Nexus 5X - 7.1.0 - API 25 - 1080x1920 vbox86p
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Android Version: 7.1.1 API: 25 LeakCanary: 1.5.4 74837f0
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Durations: watch=5476ms, gc=118ms, heap dump=6048ms, analysis=21453ms
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Details:
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: * Class com.karumi.dexter.Dexter
04-06 16:52:52.560 2742-2764/com.chess:leakcanary D/LeakCanary: |   static instance = com.karumi.dexter.DexterInstance@321527248 (0x132a1dd0)
sklinefelter commented 6 years ago

Interesting...I no longer have the leak if I comment out line ~315 of DexterInstance:

this.listener = new MultiplePermissionListenerThreadDecorator(listener, thread);

With that line on rotation, I can easily reproduce the leak.

UPDATE

@Serchinastico Please let me know if there's any more information that can be useful to you...it seems this is not isolated to MultiplePermissions, but could be caused by the amount of non-WeakReference listeners in the library. Any advice would be great.

All I had to do was add `LeakCanary' to the sample, open a permissions dialog, and watch the logs during screen rotation.

autonomousapps commented 6 years ago

@Serchinastico any updates on this? We'd love to use your library, but the only PR ever opened that addressed it ended up being closed.

Serchinastico commented 6 years ago

Hi @autonomousapps

There is an open PR fixing the issue (https://github.com/Karumi/Dexter/pull/206) but looks like it still has some flaws. However, I understand your concerns and I will try to address the issue by the end of the week.

autonomousapps commented 6 years ago

Thank you! cc/ @sklinefelter

shobhitagarwal1612 commented 5 years ago

I am also facing a similar issue. Is there any progress with this issue?