coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.84k stars 665 forks source link

Memory leak problem of ImageRequest.context #2137

Closed cwh-github closed 9 months ago

cwh-github commented 9 months ago

Android 14

Coil 2.0.0

Code:

fun showImage(imgUrl: String) { 
        val imageViewTarget = object : ImageViewTarget(imageView) {
            override fun onSuccess(result: Drawable) {
                super.onSuccess(result)
               //do something....
            }

            override fun onError(error: Drawable?) {
                super.onError(error)
                //do something
            }
        }
        val imgReq = ImageRequest.Builder(imageView.context).data(imgUrl).target(imageViewTarget).build()
        imageView.context.imageLoader.enqueue(imgReq)
    }

Logs/Screenshots

'* com.****.Activity has leaked:', 

'* GC ROOT Thread object', 

'* references android.net.ConnectivityThread (Thread.contextClassLoader)', 

'* references dalvik.system.PathClassLoader (ClassLoader.runtimeInternalObjects)', 

'* references array java.lang.Object[] (Object[3564])', 

'* references static h2.a (a.b)',   //coil.Coil  coil.ImageLoader imageLoader

'* references h2.g (g.g)',   // coil.RealImageLoader kotlinx.coroutines.CoroutineScope scope

'* references nm.e (e.a)',  //kotlinx.coroutines.internal.ContextScope  kotlin.coroutines.CoroutineContext coroutineContext

'* references ul.c (c.left)',  //kotlin.coroutines.CombinedContext  

'* references ul.c (c.left)', //kotlin.coroutines.CombinedContext 

'* references lm.t1 (j1._state)',  //kotlinx.coroutines.SupervisorJobImpl 

'* references lm.o1 (k._prev)',   //kotlinx.coroutines.NodeList

'* references lm.l (l.e)',  //kotlinx.coroutines.ChildHandleNode  kotlinx.coroutines.ChildJob childJob

'* references lm.h0 (j1._state)',  //kotlinx.coroutines.DeferredCoroutine 

'* references lm.j1$c (j1$c.a)',  //kotlinx.coroutines.JobSupport$Finishing  kotlinx.coroutines.NodeList list

'* references lm.o1 (k._next)',  //kotlinx.coroutines.NodeList 

'* references lm.l (l.e)',  //kotlinx.coroutines.ChildHandleNode  kotlinx.coroutines.ChildJob childJob

'* references lm.z1 (t.c)',  //kotlinx.coroutines.UndispatchedCoroutine  

'* references h2.h (h.L$2)',  //coil.RealImageLoader$executeMain$1 

'* references s2.h (h.a)',  //coil.request.ImageRequest  android.content.Context context

'* leaks com.******Activity instance']

Sometimes this bug occurs

colinrtwhite commented 9 months ago

Please update to the latest 2.5.0 where this should be fixed. There's a test here to ensure ImageLoader references aren't held by the network callbacks.