StrictMode: ресурс был получен при трассировке подключенного стека, но никогда не освобождается

Иногда я получаю это исключение SctrictMode:

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)

Единственная часть из упомянутого в коде исключения, принадлежащая моему приложению, 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) }!!

}

Версии библиотек, используемые в приложении:

okhttp: 3.11.0

дооснащение: 2.4.0

Что является причиной этого и как это исправить?

1 ответ

Если посмотреть на источникBridgeInterceptor, он явно создает и не закрывает его.GzipSourceреализуетSourceкоторый расширяетCloseable. Мне не ясно, действительно ли это приводит к утечке каких-либо ресурсов. В любом случае, если вы видите нарушение строгого режима, это означает, что проблема была устранена в финализаторе.

Это были okhttp 3.12.1 и okio 1.15.0. Есть гораздо более новые версии, так что это может быть исправлено.

Другие вопросы по тегам