redwarp / gifdecoder

An implementation of a gif decoder written 100% in Kotlin, plus an associated Drawable for Android
Apache License 2.0
47 stars 6 forks source link

GifWrapperDrawable set callback reference weak is unsafe #21

Closed vince-styling closed 1 year ago

vince-styling commented 1 year ago

the Drawable#setCallback() saved the instance via WeakReference, that won't last much longer, after several GC, the instance probably drop, which cause the playback stop unexpectedly.

so I make it strong reference myself.

class GifDrawable {
    private fun nextFrame(unschedule: Boolean) {
        // invalidateSelf()
        // turn to :
        strongCallback?.invalidateDrawable(this)
        // and so on
    }

    private var strongCallback: Callback? = null

    fun setStrongCallback(strongCallback: Callback) {
        this.strongCallback = strongCallback
    }

    override fun getCallback(): Callback? {
        return strongCallback
    }
}
redwarp commented 1 year ago

I think you should not do that, as it could lead to memory leak. Typically, the callback will be the view or surface or something like that, that will live as long as its on screen. You don't want to retain the callback once the view has been disposed. So, you probably don't want to do that.