5

I have certain memory leaks happening in my custom handler class ,but not sure how to fix it. checkedout a couple of examples online but nothing is specific to my code so not sure how to go about it :

private val startupCallback = object: RetryCallback(NUMBER, DELAY) {
        override fun onRetry(retryCount: Int) {

            mySdkApi.applicationStartup(this)
        }

        override fun onCompleted(): Boolean {
            updateStatus(Callback.Status.StartUpSDK)

            return true
        }

        override fun onFailed(e: MyException?) {
            updateStatus(Callback.Status.StartUpSDK, "", e)
        }
    }

Android studio keeps prompting "This handler class should be static or leaks might occur".Any ideas how to go about it?

  • 'this' == your activity? Sorry I'm not a kotlin person. I just want to know which line causes the leak. – user1506104 Nov 28 '18 at 04:48
  • non static inner classes hold a reference to an activity. As such, if the inner class stays alive then so will the activity. Make the inner class static and try to hold a WeakReference to the Activity. – Zun Nov 28 '18 at 08:57
  • Example: https://medium.com/android-stars/this-asynctask-class-should-be-static-or-leaks-might-occur-2254f3a0f18 – Zun Nov 28 '18 at 08:58
  • thanks @ZUNJAE , can you explain with respect to my code? will be really helpful and I can mark it as the correct answer –  Nov 29 '18 at 11:14

2 Answers2

1

The Android Studio complaining is pretty reasonable. The problem is that anonymous classes capture reference to the parent class that they were created in.

There are basically two solutions the "not pretty" and the ugly.) Both of them are about WeakReference.

#1 The not pretty solution is to make a class that will take a weak ref

class ApiRetryCallback(activity: Activity): RetryCallback(NUMBER, DELAY) {

    private val weakActivity = WeakReference(activity)

    override fun onRetry(retryCount: Int) {

        weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
    }

    override fun onCompleted(): Boolean {
        weakActivity.get()!!.updateStatus(Callback.Status.StartUpSDK)

        return true
    }

    override fun onFailed(e: MyException?) {
        weakActivity.get()!!.updateStatus(Callback.Status.StartUpSDK, "", e)
    }
}

In activity:

private val startupCallback = ApiRetryCallback(this) //this is MainActivity here

#2 The ugly solution is based on a fact that lambdas should capture parent reference, only where there is a direct usage of it. So I came up with this substitution and I didn't see strong references in a debugger but you should check that:

private val startupCallback = {
    val weakActivity = WeakReference(this@MainActivity)

    object : RetryCallback(NUMBER, DELAY) { //returned as last expression

        override fun onRetry(retryCount: Int) {

            weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
        }

        //....else methods....
    }

}()

Here the lambda will be called immediately and will capture only the weak reference inside the object, also it will return the last expression wich is object.

#3 While I was writing, I came up with a third solution, which is close to #2

private val startupCallback = WeakReference(this).let { //this here is MainActivity
    val weakActivity = it //it of let scope wich is WeakReference

    object : RetryCallback(NUMBER, DELAY) { //returned as last expression

        override fun onRetry(retryCount: Int) {

            weakActivity.get()!!.mySdkApi.applicationStartup(this) //or weakThis.get()? to swallow null cases
        }

        //....else methods....
    }

}
While True
  • 423
  • 2
  • 15
  • wont let me offer you this bounty for the next 16 hours, will give it to you then –  Nov 30 '18 at 18:34
  • btw do you happen to know how to solve this : https://stackoverflow.com/questions/53547984/content-inside-webview-popup-is-hidden-behind-keyboardunique-case-please-read?noredirect=1#comment93963353_53547984 –  Nov 30 '18 at 18:34
0

Anonymous classes (like yours) are non static. You can replace anonymous class with the normal class (just create class extending RetryCallback) and pass all needed objects as constructor arguments.

  • 1
    That will make a leak smaller, but if the internal object leaked so will the separate class. The right answer is to figure out where the reference that caused it to leak was, and either get rid of that reference totally, or at least when the object should go out of scope – Gabe Sechan Nov 30 '18 at 13:49