2

I want to use StrictMode with .detectAll() and .penaltyDeath() to discover problems in my app. But it's very common, that my app is terminated (because of penaltyDeath) with this message:

D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks. Callsite: close

To find the cause, I have removed almost all the code from my app - but without success.

I was surprised to find that I can recreate this problem in the automatically generated project "Basic Activity" from Android Studio.

And now I notice that this problem only occurs in the Android 11 emulator. It does not occur in the Android 8.1 image.

How to reproduce:

  1. In Android Studio: File > New > New Project > Basic Activity > Kotlin + API 21

  2. Add

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    StrictMode.setVmPolicy(
        StrictMode.VmPolicy.Builder()
        .detectLeakedClosableObjects()
        .penaltyLog()
        .penaltyDeath()
        .build())
}

to the onCreate-method between super.onCreate(savedInstanceState) and binding = ActivityMainBinding.inflate(layoutInflater)

  1. Add missing imports

  2. Run app

  3. Wait until the app is started/launch in the emulator

  4. Run again

  5. Wait 5 seconds

  6. App crashes with D/StrictMode: StrictMode policy violation: android.os.strictmode.LeakedClosableViolation: A resource was acquired at attached stack trace but never released...

  7. (Sometimes penaltyDeath() kills too fast and the exception is not written to Logcat)

I already:

  • recreated and cold booted the emulator
  • x86 and x86_64 of Android R are effected

I'm using Linux, Manjaro, Ryzen 7 5800X, 32GB RAM. Does anyone have an idea what I'm doing wrong?

Log from Android Studio (from Run tab) https://pastebin.com/fkq0u1vd

  • 1
    Seems to be a duplicate of https://stackoverflow.com/questions/65011420/strictmode-policy-violation-android-os-strictmode-leakedclosableviolation-in-my – Tobias Hellmann Sep 28 '21 at 11:37

0 Answers0