coil-kt / coil

Image loading for Android and Compose Multiplatform.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NPE in SystemCallbacks.onTrimMemory

Jeff11 opened this issue · comments

Got a few crashes in 2.0.0-rc01 on Android 8-12.
Firebase reports that app was always in background

Fatal Exception: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object sb.d.getValue()' on a null object reference
       at coil.RealImageLoader.getMemoryCache(RealImageLoader.kt:80)
       at coil.RealImageLoader.onTrimMemory$coil_base_release(RealImageLoader.java:213)
       at coil.util.SystemCallbacks.onTrimMemory(SystemCallbacks.kt:52)
       at android.app.Application.onTrimMemory(Application.java:306)
       at android.app.ActivityThread.handleTrimMemory(ActivityThread.java:6653)
       at android.app.ActivityThread.access$1400(ActivityThread.java:274)
       at android.app.ActivityThread$ApplicationThread.lambda$scheduleTrimMemory$0(ActivityThread.java:1742)
       at android.app.-$$Lambda$ActivityThread$ApplicationThread$tUGFX7CUhzB4Pg5wFd5yeqOnu38.accept(-.java:8)
       at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:271)
       at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:195)
       at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:86)
       at android.os.Handler.handleCallback(Handler.java:883)
       at android.os.Handler.dispatchMessage(Handler.java:100)
       at android.os.Looper.loop(Looper.java:237)
       at android.app.ActivityThread.main(ActivityThread.java:8167)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

Hmm are you using an alpha version of the Android Gradle Plugin? Looking at the code it shouldn't be possible to trigger an NPE here without R8 manipulating the code incorrectly.

Are you using any custom R8 rules that could rewrite this code as well?

Android Gradle Plugin version is 7.1.2

The R8 rules have nothing special I think, but here they are

removed

None of those rules look like they should cause an NPE in Coil. Interestingly we're seeing the same crash at Instacart, though very low volume. My suspicion is R8 is manipulating the code incorrectly since it only occurs in release builds and we're doing a null check.

Opened #1215 to hopefully guard against the crash. I don't see anything related in the R8 bug tracker, but I'd like to figure out the root cause instead of just working around it if possible.

It seems to occur when the application is started via a push notification for us. Still unsure about the root cause (seems to be outside Coil's control), but I've added a work-around for the crash in the next release.

Last occurrence was on April 4th. Always when the App is in the background, most likely it is triggered with a push notification, yes.

Closing this out as the work-around seems to have fixed the issue (exceptions have stopped for us). My suspect for the root cause is still R8 given this only occurs in release builds. I'll revisit the work-around later and see if it's possible to be removed.