2

Hi I'm using the following code so in the event of a crash recycle will always be called to free up the memory.

context.obtainStyledAttributes(attrs, R.styleable.example).apply {
    try {
        getDrawable(R.styleable.example_drawable)
    } finally {
        recycle()
    }
}

However, for some reason in android studio lint cannot detect the recycle call and it always complains, giving me this warning:

This 'TypedArray' should be recycled after use with '#recycle()'

I also tried using a Kotlin extension function which should automatically call recycle for me:

context.obtainStyledAttributes(attrs, R.styleable.example).use
{
    it.getDrawable(R.styleable.example_drawable)
}

But the lint warning still persists. Any idea what to do?

I am currently using latest versions of Android Studio, Gradle, and Kotlin (3.5.2, 6.0.1, and 1.3.60 respectively)

Is there a way to update lint maybe? Or does it already come with Android Studio.? Thanks.

*Edit: It seems the .apply is part of the problem. Removing that or the try finally block removes the warning, so somehow you can't have both at the same time. Is this a bug..?

Joe MM
  • 21
  • 2
  • Looks similar to https://stackoverflow.com/q/57225133/8298909. My guess is that the linter just isn't sophisticated enough to "know" that you're doing the right thing. – Ben P. Dec 03 '19 at 20:13

0 Answers0