StrictMode: A resource was acquired at attached stack trace but never released
Asked Answered
M

1

7

Sometimes I get this SctrictMode Exception:

2019-01-28 14:18:23.073 4597-4615/my.app.package E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'end' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:223)
        at java.util.zip.Inflater.<init>(Inflater.java:106)
        at okio.GzipSource.<init>(GzipSource.java:62)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:103)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:213)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at my.app.package.api.network.SetJsonContentTypeHeaderInterceptor.intercept(SetJsonContentTypeHeaderInterceptor.kt:15)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

The only part of the mentioned in the exception code that belong to my app is SetJsonContentTypeHeaderInterceptor:

import okhttp3.Interceptor

internal class SetJsonContentTypeHeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain) =
        chain.request()
            .let {
                it.newBuilder()
                    .header("Content-Type", "application/json")
                    .header("Accept", "application/json")
                    .build()
            }
            .let { chain.proceed(it) }!!

}

The libraries versions used in the app:

okHttp: 3.11.0

retrofit: 2.4.0

What is a cause of it and how to fix it?

Mosher answered 30/1, 2019 at 11:27 Comment(0)
P
0

If you look at the source of BridgeInterceptor, it clearly creates a GzipSource and doesn't close it. GzipSource implements Source which extends Closeable. It's not clear to me that this actually leaks any resources. Regardless, if you're seeing the strictmode violation it means the problem was cleaned up in the finalizer.

This was okhttp 3.12.1 and okio 1.15.0. There are much newer versions so this may be fixed.

Peba answered 5/1, 2023 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.