Android: "trying to use a recycled bitmap" error with temporary Bitmaps
Asked Answered
P

1

20

My app can load quite large images. In an effort to be memory-conservative, I'm attempting to use a temporary bitmap to load and another for the final image after transformation:

.....
finalBitmap.recycle();
finalBitmap = null;
Bitmap tempBitmap  = BitmapFactory.decodeStream(fin, ...);
finalBitmap = Bitmap.createBitmap(tempBitmap, ....);
imgview.setImageBitmap(finalBitmap);
.....

Now, at this point we're done with tempBitmap, which was only needed to transport the decoded Bitmap to the transformation step in createBitmap. So:

.....
tempBitmap.recycle();
tempBitmap = null;
.....

And... it crashes with a "trying to use a recycled bitmap" error specifically because of the recycling of tempBitmap. tempBitmap wasn't displayed and is only used right there.

What's going wrong here? Should I just use "finalBitmap" throughout and rely on createBitmap to manage it (finalBitmap = Bitmap.createBitmap(finalBitmap , ....))? I fail to see what ongoing dependency on tempBitmap there would be that would cause such a failure.

Edit: Yes, the null assignment seems to result in the appropriate, eventual garbage collection, but I'm mystified as to why recycle() on a temp Bitmap is so problematic in this case. I get the impression that createBitmap() is holding a reference to it but why, and for how long?

Peradventure answered 2/11, 2011 at 15:53 Comment(0)
W
25

Straight from the Android documentation:

Returns an immutable bitmap from the specified subset of the source bitmap. The new bitmap may be the same object as source, or a copy may have been made.

It seems that the createBitmap functions have the potential to re-use the bitmap that you provided. If that is the case, then you shouldn't recycle the temporary bitmap since your final bitmap is using it. One thing you can do is

if(tempBitmap != finalBitmap) {
   tempBitmap.recycle();
}

That should only recycle the tempBitmap when it isn't the same as the finalBitmap. At least that seems to be what the documentation is implying.

Willawillabella answered 2/11, 2011 at 17:1 Comment(1)
I missed the bit about it potentially being the same object. My setting the temp to null doesn't cause side-effects because it doesn't really release the object, while recycle() directly affects it. Hmm. I accept your answer and hope you'll do me the favor of upvoting my question (since I think it's a useful bit of info for people).Peradventure

© 2022 - 2024 — McMap. All rights reserved.