smoothie icon indicating copy to clipboard operation
smoothie copied to clipboard

RuntimeException: Canvas: trying to use a recycled bitmap

Open Flitskikker opened this issue 12 years ago • 8 comments

Hi,

I'm trying to use your wonderful Smoothie library in a new project, based on the bitmapcache example. It works in the emulator, but on my device (S2 @ Stock 4.0.4), it crashes when I scroll the ListView.

Maybe it has something to do with the bitmapcache fade in thingy?

Thanks in advance for checking out.

Cheers, Martijn

02-09 02:57:41.600 E/AndroidRuntime(18033): FATAL EXCEPTION: main 02-09 02:57:41.600 E/AndroidRuntime(18033): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@415aaf40 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.Canvas.throwIfRecycled(Canvas.java:1038) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.Canvas.drawBitmap(Canvas.java:1142) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:394) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:198) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ImageView.onDraw(ImageView.java:910) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11184) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ListView.drawChild(ListView.java:3231) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.AbsListView.dispatchDraw(AbsListView.java:2277) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.ListView.dispatchDraw(ListView.java:3226) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.AbsListView.draw(AbsListView.java:3760) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.FrameLayout.draw(FrameLayout.java:450) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2892) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.drawChild(ViewGroup.java:2890) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.View.draw(View.java:11187) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.widget.FrameLayout.draw(FrameLayout.java:450) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2291) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2210) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.os.Handler.dispatchMessage(Handler.java:99) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.os.Looper.loop(Looper.java:137) 02-09 02:57:41.600 E/AndroidRuntime(18033): at android.app.ActivityThread.main(ActivityThread.java:4511) 02-09 02:57:41.600 E/AndroidRuntime(18033): at java.lang.reflect.Method.invokeNative(Native Method) 02-09 02:57:41.600 E/AndroidRuntime(18033): at java.lang.reflect.Method.invoke(Method.java:511) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 02-09 02:57:41.600 E/AndroidRuntime(18033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 02-09 02:57:41.600 E/AndroidRuntime(18033): at dalvik.system.NativeStart.main(Native Method)

Flitskikker avatar Feb 09 '13 02:02 Flitskikker

Tested some more. In the ListLoader, displayItem(); if I replace the transition code:

BitmapDrawable emptyDrawable = new BitmapDrawable(itemView.getResources());

TransitionDrawable fadeInDrawable = new TransitionDrawable(new Drawable[] { emptyDrawable, result });

holder.image.setImageDrawable(fadeInDrawable);
fadeInDrawable.startTransition(200);

by simply:

holder.image.setImageDrawable(result);

...the application doesn't crash on my phone.

However, I tried your sample (including the fade ins) and it doesn't crash. Did I do something wrong?

Flitskikker avatar Feb 09 '13 02:02 Flitskikker

So what happened here? I still get the same error as Flitskikker and can only fix it with the same workaround. How can i not run into this while still fading in the image?

atla avatar Feb 23 '13 08:02 atla

Oh - just a quick hint... i updated to the latest BitmapCache version from git that also includes setRecyclePolicy on the builder (i was curious to try that out) and the crash seems to be gone. It internally also uses a newer version of the DiskLRUCache.

atla avatar Feb 23 '13 08:02 atla

I updated the BitmapCache library from Git already a while ago, and it would still crash on my device. It works fine on the emulator (4.2), though. Since I've updated my device to JB 4.2.2, it now also works on there.

I've just updated the library again and it still crashes when scrolling on an emulator running 2.2. Can't test it on my device now unfortunately, but I suppose it would still crash (on 4.0.4). Same workaround: disable the fade ins and it works.

@atla: A few questions:

  • Did you apply a different recycle policy after which the crash was fixed, or was it fixed by just updating the BitmapCache library?
  • Which Android version are you testing it on? You might want to test it on e.g. 2.2 too to see if the problem still persists.

I'm using the BitmapCache from GIT and the DiskLRUCache 1.3.1 jar (not the GIT source as library).

Thanks in advance!

Flitskikker avatar Feb 23 '13 15:02 Flitskikker

Hi,

sorry for taking so long to respond. Actually the issue still occured to me. It didnt when i answered this post so i thought its fixed. But it is still occuring randomly. The only "fix" that i have - what i currently activated - is the Recycle policy RecyclePolicy.DISABLED ... which i guess has a huge impact on memory and basically makes the caching useless.

Still not sure where exactly this bug comes from.. sorry.

regards, marcus

atla avatar Feb 27 '13 11:02 atla

Hi,

I fixed this problem, first switch ImageView by CacheableImageView. The BitmapCache need CacheableImageView to recycle correctly.

So, ViewHolder should be: class ViewHolder { public CacheableImageView image; public TextView title; }

And don't forget on layout: <uk.co.senab.bitmapcache.CacheableImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" />

And second, you need remove TransitionDrawable (see Flitskikker comment above), again the BitmapCache need CacheableImageView to recycle correctly.

I hope this helps.

Thanks.

jansouza avatar Mar 12 '13 05:03 jansouza

I'm getting this error randomly as well. @jansouza How does the TransitionDrawable have anything to do with the way recycling happens?

*** UPDATE 4/25 *** I've removed the TransitionDrawable code and this still seems to happen. It seems to happen less but I've noticed it happen a few times on my testing device (Hauwei Comet).

jerrellmardis avatar Apr 25 '13 03:04 jerrellmardis

Had the same issue, Flitskikker's solution of removing the transition worked for me.

blaztriglav avatar May 15 '13 09:05 blaztriglav