square / leakcanary

A memory leak detection library for Android.
https://square.github.io/leakcanary
Apache License 2.0
29.42k stars 3.97k forks source link

Confirmed leak caused by Audio Manager and VideoView. #205

Closed sbaar closed 8 years ago

sbaar commented 9 years ago

07-06 17:15:55.110 29862-30848/ D/LeakCanary﹕ * GC ROOT android.media.AudioManager$1.this$0 (anonymous class extends android.media.IAudioFocusDispatcher$Stub) 07-06 17:15:55.110 29862-30848/ D/LeakCanary﹕ * references android.media.AudioManager.mContext 07-06 17:15:55.110 29862-30848/ D/LeakCanary﹕ * references android.app.ContextImpl.mOuterContext

07-06 17:15:55.110 29862-30848/D/LeakCanary﹕ * Device: asus google Nexus 7 razor 07-06 17:15:55.110 29862-30848/D/LeakCanary﹕ * Android Version: 5.1.1 API: 22 LeakCanary: 1.3.1

This bug has been acknowledged https://code.google.com/p/android/issues/detail?id=152173 and fixed https://android-review.googlesource.com/#/c/140481/1 but not currently as of SDK 22.

It can be ignored with builder.instanceField("android.media.AudioManager","mContext"), but that only causes the log ouput to say that the leak can be ignored and not cause the canary notification to pop up in the tray. Is this the extent of ignoring a leak or is there a way to not cause the app to freeze (brrr) when detecting a leak that should be ignored?

jankovd commented 9 years ago

Just noticed this same leak today. To fix the leak, I wrap the Activity context in a ContextWrapper and use the Application context when the AudioManager is requested by getSystemService(AUDIO_SERVICE) Use it as you see fit https://gist.github.com/jankovd/891d96f476f7a9ce24e2