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

java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@2dad25ee #1726

Closed
minji19 opened this issue Apr 18, 2017 · 4 comments

Comments

@minji19
Copy link

@minji19 minji19 commented Apr 18, 2017

Description

The BitmapPool log below:
04-18 10:39:34.797 21848-21984/? V/unknown:BitmapPool: release (free) (object, size) = (37535584, 16384)
04-18 10:39:34.797 21848-21984/? V/unknown:BitmapPool: Used = (256, 10787136); Free = (0, 0)
04-18 10:39:34.797 21848-21977/? V/unknown:BitmapPool: Used = (257, 10803520); Free = (0, 0)
04-18 10:39:34.797 21848-21977/? V/unknown:BitmapPool: get (alloc) (object, size) = (84036b7, 16384)
04-18 10:39:34.797 21848-21977/? V/unknown:BitmapPool: release (free) (object, size) = (39308e33, 16384)
04-18 10:39:34.797 21848-21977/? V/unknown:BitmapPool: Used = (256, 10787136); Free = (0, 0)
04-18 10:39:34.807 21848-21982/? V/unknown:BitmapPool: Used = (257, 10803520); Free = (0, 0)
04-18 10:39:34.807 21848-21982/? V/unknown:BitmapPool: get (alloc) (object, size) = (1bf43b42, 16384)
04-18 10:39:34.807 21848-21982/? V/unknown:BitmapPool: release (free) (object, size) = (2dad25ee, 16384)
04-18 10:39:34.807 21848-21982/? V/unknown:BitmapPool: Used = (256, 10787136); Free = (0, 0)
04-18 10:39:34.807 21848-21975/? V/unknown:BitmapPool: Used = (257, 10803520); Free = (0, 0)
04-18 10:39:34.807 21848-21975/? V/unknown:BitmapPool: get (alloc) (object, size) = (35708589, 16384)
04-18 10:39:34.817 21848-21975/? V/unknown:BitmapPool: release (free) (object, size) = (afcb37f, 16384)
04-18 10:39:34.817 21848-21975/? V/unknown:BitmapPool: Used = (256, 10787136); Free = (0, 0)
04-18 10:39:34.817 21848-21976/? V/unknown:BitmapPool: Used = (257, 10803520); Free = (0, 0)
04-18 10:39:34.817 21848-21976/? V/unknown:BitmapPool: get (alloc) (object, size) = (5882dbc, 16384)
04-18 10:39:34.817 21848-21976/? V/unknown:BitmapPool: release (free) (object, size) = (1f80c125, 16384)
04-18 10:39:34.817 21848-21976/? V/unknown:BitmapPool: Used = (256, 10787136); Free = (0, 0)

Then there was a crash.This happens when I use ImagePipeline directly while repeating the same picture but rewriting the CacheKey in PostProcess for not allow it to use bitmap in memory. Because it can be reproduced this crash.The code is below:

imagePipeline.fetchDecodedImage(request, null);

@Override
            protected void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                if (!dataSource.isFinished()) {
                    onFailureImpl(dataSource);
                    return;
                }

                CloseableReference<CloseableImage> result = dataSource.getResult();
                if (result != null) {
                    listener.success(new FrescoBitmapWrapper(result));
                    result.close();
                }
            }

Here i clone the result in FrescoBitmapWrapper's constructor.And then put the bitmap in the ImageView.

Additional Information

  • Fresco version: [1.0]
  • Platform version: [android 4.0,5.0,6.0]
@massimocarli massimocarli added the bug label Apr 18, 2017
@massimocarli

This comment has been minimized.

Copy link
Contributor

@massimocarli massimocarli commented Apr 18, 2017

Hi @MinJiLiu
we'll have a look.
Thanks

@oprisnik

This comment has been minimized.

Copy link
Contributor

@oprisnik oprisnik commented May 5, 2017

How are you cloning the bitmap and using it? I suspect that the underlying bitmap is closed.

@minji19

This comment has been minimized.

Copy link
Author

@minji19 minji19 commented May 8, 2017

public FrescoBitmapWrapper(CloseableReference<CloseableImage> ref) {
        mReference = ref.clone();
}

@Override
@Deprecated
public synchronized Bitmap get() {
    if (mReference != null) {
        CloseableImage image = mReference.get();
        if (image != null && image instanceof CloseableBitmap)
        return ((CloseableBitmap) image).getUnderlyingBitmap();
    }
    return null;
}

@Override
public void release() {
    if (release) {
        return;
    }
    CloseableReference.closeSafely(mReference);
    mReference = null;
    release = true;
}

@Override
public void setBitmap(ImageView imageView) {
    Bitmap bm = get();
    imageView.setImageBitmap(bm);
    Object tag = imageView.getTag(R.id.tag_key);
    if (tag != null && tag instanceof FrescoBitmapWrapper) {
        FrescoBitmapWrapper wrapper = (FrescoBitmapWrapper) tag;
        wrapper.release();
    }
    imageView.setTag(R.id.tag_key, this);
    imageView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View v) {
         }
         @Override
        public void onViewDetachedFromWindow(View v) {
            release();
        }
});
@oprisnik

This comment has been minimized.

Copy link
Contributor

@oprisnik oprisnik commented Jul 11, 2017

I think the issue here is that you re-attach the imageView, which will result in the bitmap being closed for the detach event and re-attaching causes the recycled bitmap to be drawn, which throws the exception. You have to correctly handle closeable references as described in http://frescolib.org/docs/closeable-references.html

@oprisnik oprisnik closed this Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.