Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strictmode sometimes detects leaks on FileOutputStream #115

Closed
Trikke opened this issue Dec 4, 2012 · 7 comments
Closed

Strictmode sometimes detects leaks on FileOutputStream #115

Trikke opened this issue Dec 4, 2012 · 7 comments
Labels

Comments

@Trikke
Copy link

Trikke commented Dec 4, 2012

Hi,

first of all, big fan of the library! Works really well and was easy to integrate. But sometimes when an image is read from diskcache, strictmode throws a warning :

12-04 14:17:55.677: ERROR/StrictMode(25142): 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 'close' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:184)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:90)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:286)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:204)
        at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)

These are my settings for UIL :

DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage( R.drawable.img_placeholder )
    .showImageForEmptyUri( R.drawable.img_placeholder )
    .resetViewBeforeLoading()
    .cacheInMemory()
    .cacheOnDisc()
    .imageScaleType( ImageScaleType.IN_SAMPLE_POWER_OF_2 )
    .bitmapConfig( Bitmap.Config.RGB_565 )
    .displayer( new FadeInBitmapDisplayer( 200 ) )
    .build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( getApplicationContext() )
    .memoryCacheExtraOptions( maxDimension, maxDimension ) // max width, max height
    .discCacheExtraOptions( maxDimension, maxDimension, Bitmap.CompressFormat.JPEG, 75 )
    .threadPoolSize( 3 )
    .threadPriority( Thread.NORM_PRIORITY - 1 )
    .defaultDisplayImageOptions( options )
    .offOutOfMemoryHandling()
    .memoryCache( new UsingFreqLimitedMemoryCache( cacheSize ) )
    .discCache( new UnlimitedDiscCache( cacheDir ) )
    .discCacheFileNameGenerator( new HashCodeFileNameGenerator() )
    .imageDownloader( new URLConnectionImageDownloader( 5 * 1000, 20 * 1000 ) ) // connectTimeout (5 s), readTimeout (20 s)
    .tasksProcessingOrder( QueueProcessingType.FIFO )
    .enableLogging()
    .build();
@nostra13
Copy link
Owner

nostra13 commented Dec 6, 2012

First of all, your configuration is equal to

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( getApplicationContext() )
    .memoryCacheExtraOptions( maxDimension, maxDimension ) // max width, max height
    .discCacheExtraOptions( maxDimension, maxDimension, Bitmap.CompressFormat.JPEG, 75 )
    .defaultDisplayImageOptions( options )
    .offOutOfMemoryHandling()
    .memoryCacheSize( cacheSize )
    .discCache( new UnlimitedDiscCache( cacheDir ) )
    .enableLogging()
    .build();

and I think you can also delete .memoryCacheSize(...) and .discCache(...) calls.

About warning: it seems to be ma fault, I didn't close OutputStream after saving image to disc cache. I thought Bitmap.compress(...) do it but most likely I was mistaken.
Thank you, I'll fix it in next version.

@Trikke
Copy link
Author

Trikke commented Dec 7, 2012

Hi Nostra,

thanks for that config info, i didn't know all the default values so i just set about everything. Also thanks for the eventual fix that will be created.

I've seem some other strictmode errors in other cases as well. I'll post my findings here as well.

@henrikpersson
Copy link

@nostra13 Here's a related strictmode stacktrace:

12-11 13:55:32.580: E/StrictMode(2468): 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:187)
    at java.util.zip.Inflater.<init>(Inflater.java:82)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.initContentStream(HttpURLConnectionImpl.java:550)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getFromNetwork(HttpURLConnectionImpl.java:1094)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1041)
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:523)
    at com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader.getStreamFromNetwork(URLConnectionImageDownloader.java:40)
    at com.nostra13.universalimageloader.core.download.ImageDownloader.getStream(ImageDownloader.java:25)
    at com.nostra13.universalimageloader.core.ImageDecoder.computeImageScale(ImageDecoder.java:113)
    at com.nostra13.universalimageloader.core.ImageDecoder.getBitmapOptionsForImageDecoding(ImageDecoder.java:101)
    at com.nostra13.universalimageloader.core.ImageDecoder.decode(ImageDecoder.java:79)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeWithOOMHandling(LoadAndDisplayImageTask.java:186)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:169)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:145)
    at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:72)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
    at java.lang.Thread.run(Thread.java:1020)

@adgeg
Copy link

adgeg commented Jan 30, 2014

Hello,

First of all, thank you for your amazing work. Your library is clearly my favorite one for image loading, I include it in nearly all my projects.

I am currently using StrictMode in one of them. With version 1.9.1 of your library, the previous exception is still raised (after a FileNotFoundException):

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:184)
            at java.util.zip.Inflater.<init>(Inflater.java:82)
            at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:96)
            at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
            at libcore.net.http.HttpEngine.initContentStream(HttpEngine.java:547)
            at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:850)
            at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
            at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:106)
            at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:77)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:319)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisc(LoadAndDisplayImageTask.java:298)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:241)
            at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:141)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:849)

These are my settings for UIL:

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .displayer(new FadeInBitmapDisplayer(
                        getResources().getInteger(android.R.integer.config_shortAnimTime)))
                .build();
        ImageLoaderConfiguration config =
                new ImageLoaderConfiguration.Builder(getApplicationContext())
                        .defaultDisplayImageOptions(defaultOptions)
                        .build();
        ImageLoader.getInstance().init(config);

Temporarily, I will remove detectLeakedClosableObjects() from StrictMode.

Thanks for your help.

@nostra13
Copy link
Owner

Hello,
It's strange because I'm pretty sure I considered all possible exceptions and always close opened streams.

@noinnion
Copy link

noinnion commented Feb 2, 2014

I also experience the StrictMode exception:

A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource
java.lang.Throwable: Explicit termination method 'end' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:184)
at java.util.zip.Inflater.(Inflater.java:82)
at java.util.zip.GZIPInputStream.(GZIPInputStream.java:96)
at java.util.zip.GZIPInputStream.(GZIPInputStream.java:81)
at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:468)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:666)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:106)
at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:77)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.downloadImage(LoadAndDisplayImageTask.java:319)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryCacheImageOnDisc(LoadAndDisplayImageTask.java:298)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:241)
at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:141)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

@noinnion
Copy link

Hi @nostra13

it seems to be a bug in okhttp / retrofit
square/okhttp#281

maybe you should update to the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants