FFImageLoading icon indicating copy to clipboard operation
FFImageLoading copied to clipboard

Error applying Android Transform to image

Open goneale opened this issue 11 years ago • 25 comments

With this code inside override void OnBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) of a RecyclerView.Adapter:

ImageService.LoadUrl(item.ImageUrl)
               .Retry(3, 200)
               .DownSample(1080)
               .Transform(new BlurredTransformation(24f))
               .Into(vh.Image);

When I press the back button in my Android Toolbar or even intermittently on load, I receive the following exception which crashes the app soon after displaying the blurred effect:

12-01 22:36:21.497 E/mono-rt (24120): [ERROR] FATAL UNHANDLED EXCEPTION: Java.Lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@529be970
12-01 22:36:21.497 E/mono-rt (24120):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2098/3efa14c4/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
12-01 22:36:21.497 E/mono-rt (24120):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 
12-01 22:36:21.497 E/mono-rt (24120):   at Android.Graphics.Drawables.BitmapDrawable.Draw (Android.Graphics.Canvas canvas) [0x00070] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Graphics.Drawables.BitmapDrawable.cs:619 
12-01 22:36:21.497 E/mono-rt (24120):   at FFImageLoading.Work.FFBitmapDrawable.Draw (Android.Graphics.Canvas canvas) [0x00019] in <filename unknown>:0 
12-01 22:36:21.497 E/mono-rt (24120):   at Android.Graphics.Drawables.BitmapDrawable.n_Draw_Landroid_graphics_Canvas_ (IntPtr jnienv, IntPtr native__this, IntPtr native_canvas) [0x00011] in /Users/builder/data/lanes/2098/3efa14c4/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Graphics.Drawables.BitmapDrawable.cs:601 
12-01 22:36:21.497 E/mono-rt (24120):   at (wrapper dynamic-method) System.Object:b8009a04-c092-4fa4-ae64-cc34b063b436 (intptr,intptr,intptr)
12-01 22:36:21.497 E/mono-rt (24120):   --- End of managed exception stack trace ---
12-01 22:36:21.497 E/mono-rt (24120): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@529be970
12-01 22:36:21.497 E/mono-rt (24120):   at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1084)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:844)
12-01 22:36:21.497 E/mono-rt (24120):   at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:490)
12-01 22:36:21.497 E/mono-rt (24120):   at md564c4a2b9f24024bbfe8a5a73e9baebd9.FFBitmapDrawable.n_draw(Native Method)
12-01 22:36:21.497 E/mono-rt (24120):   at md564c4a2b9f24024bbfe8a5a73e9baebd9.FFBitmapDrawable.draw(FFBitmapDrawable.java:94)
12-01 22:36:21.497 E/mono-rt (24120):   at android.widget.ImageView.onDraw(ImageView.java:1037)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14465)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13362)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14468)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13362)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:3693)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14468)
12-01 22:36:21.497 E/mono-rt (24120):   at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3171)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13362)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13357)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13357)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13357)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13404)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.draw(View.java:14182)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
12-01 22:36:21.497 E/mono-rt (24120):   at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:13357)
12-01 22:36:21.497 E/mono-rt (24120):   at android.view.View.getDisplayList(View.java:1

goneale avatar Dec 02 '15 04:12 goneale

If you know some exact steps that lead to that. Could you write them? Also could you indicate your Android version and device?

molinch avatar Dec 02 '15 11:12 molinch

Btw I tried to repro this exact issue during 1h and didn't managed to have it. If you can detail as much as possible that's gonna help for debugging. Thanks

molinch avatar Dec 02 '15 21:12 molinch

Try swapping out the standard image display for FFImageLoading in this sample, that is the Xamarin template I started with. I do believe it's an issue in conjunction with recycler views.
Update - Sorry link is: https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewer

goneale avatar Dec 02 '15 23:12 goneale

Which Android version?

molinch avatar Dec 03 '15 07:12 molinch

4.4.4

goneale avatar Dec 03 '15 22:12 goneale

Have also received this error. Android 6, Xamarin Forms 1.5.1.6471, FFImageLoading 2.0.0-alpha1

[Bitmap] Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue*) <0x000e7>
[MonoDroid] at Android.Graphics.Drawables.BitmapDrawable.Draw (Android.Graphics.Canvas) <0x001cb>
[MonoDroid] at FFImageLoading.Work.FFBitmapDrawable.Draw (Android.Graphics.Canvas) <0x00033>
[MonoDroid] at Android.Graphics.Drawables.BitmapDrawable.n_Draw_Landroid_graphics_Canvas_ (intptr,intptr,intptr) <0x0005b>
[MonoDroid] at (wrapper dynamic-method) object.5fb8ea0e-2829-401d-a1cb-cc397f174bf7 (intptr,intptr,intptr) <0x00043>
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@d0b1e9e

ahohns avatar Dec 04 '15 05:12 ahohns

@ahohns Can you give me some code to reproduce Xamarin.Forms bug? I'll Debug it. Do you use transformations?

daniel-luberda avatar Dec 04 '15 06:12 daniel-luberda

I can't at the moment sorry, as I only just noticed it for the first time this afternoon testing a new beta release. Yep using a custom transformation.

While we're only on XF 1.5.1 we're using TwinTechs FastCell in a ListView which uses recycling.

ahohns avatar Dec 04 '15 06:12 ahohns

@ahohns If you're using a custom transformation, be sure that you don't dispose/recycle source bitmap. It's really important as FFImageLoading does it after transformation. I'm quite sure it's the cause.

In old versions of FFImageLoading bitmap recycling had to be handled inside custom transformation's Transform method, but from a few versions it's done outside of it automatically and you shouldn't do that. I think it's since first Transformations nuget package release.

daniel-luberda avatar Dec 04 '15 06:12 daniel-luberda

Hmm I don't believe we're doing anything with the source, it's a simple transformation to create a circle image with a border which pretty much follows existing transformations:

protected override Bitmap Transform (Bitmap source)
        {
            double size = source.Width >= _width ? source.Width : _width * App.ScreenScale;

            float rad = (float)(Math.Min(size, size) / 2);

            Bitmap output = Bitmap.CreateBitmap((int)size, (int)size, Bitmap.Config.Argb8888);
            using (Canvas canvas = new Canvas(output))
            using (Paint paint = new Paint ())
            using (BitmapShader shader = new BitmapShader (source, Shader.TileMode.Clamp, Shader.TileMode.Clamp))
            using (RectF rectF = new RectF(BorderThickness/2, BorderThickness/2, (int)size - BorderThickness/2, (int)size - BorderThickness/2))
            {
                paint.AntiAlias = true;
                paint.SetShader (shader);

                canvas.DrawRoundRect (rectF, rad, rad, paint);

                if (BorderThickness > 0) {
                    paint.Color = BorderColor.ToAndroid ();
                    paint.SetStyle (Paint.Style.Stroke);
                    paint.StrokeWidth = BorderThickness;
                    paint.SetShader (null);
                    canvas.DrawRoundRect (rectF, rad, rad, paint);
                }

                return output;              
            }
        }

ahohns avatar Dec 04 '15 06:12 ahohns

I just tested it on a large list and couldn't reproduce it. Your transformation works without any exception. If it's some threading issue - you could add a lock(source) on entire Transform method body, maybe it would help. Let me know if it would fix it.

daniel-luberda avatar Dec 04 '15 06:12 daniel-luberda

Did you guys got it in simulator too? If yes could you tell me your config? (So I can match it)

molinch avatar Dec 04 '15 06:12 molinch

@ahohns @goneale Could you give more info? Do you still have the issue btw? If you have a repro sample would it be OK to share it? (can be done by email for privacy reasons)

molinch avatar Dec 11 '15 07:12 molinch

I was able to almost always repro it this morning (using alpha-5) but another exception was happening around the same time and once I fixed that I didn't have a way to repro it any more. Perhaps it was the way we were doing something and changing it fixed it. We're using Xamarin Insights with our testers so if I see the issue still happening we'll try and track it down again.

ahohns avatar Dec 11 '15 07:12 ahohns

@molinch @daniel-luberda Did some testing using alpha-6 on a slow device today and finally got this error again. Unfortunately it was totally random, but it's possible there's some sort of corruption going on and looking at Xamarin Insights the devices we see this error from are slower / older devices. We're using Xamarin 1.5.1

In rare cases after clicking around the app really fast (especially different pages that load heaps of images) we'll go back to the root page and there will be random images appearing in the wrong place almost as if the wrong images are being loaded back in the wrong place. This is what happened today when I received the error .. our root page has a background image that's a CachedImage loaded from ImageSource.File and it never changes, but when returning back from another page the background image had changed to an image that's dynamically loaded (ImageSource.FromUri) and the place where the dynamically loaded image was from was now blank. A few seconds later is when the app crashed.

Hard to explain and unfortunately unable to reliably reproduce other than click around like mad, but there's definitely something strange happening. We're under a deadline here so I can't put much time into it, but after that I can try and help hunt it down.

I don't suppose the error could at least be caught in FFBitmapDrawable.Draw() so it doesn't cause a fatal exception?

ahohns avatar Dec 14 '15 06:12 ahohns

Many thanks for investigating it @ahohns On Android bitmap memory is not reclaimed in a deterministic way. Due to that memory will grow until GC kicks in. Currently we force GC only when getting an OutOfMemoryException, which oftently only delays the issue. Therefore when the memory is really too low Android kills the app and there is no way to catch it. I believe that is what happens here. I could be wrong but I don't think this is catchable.

The best would be to profile the app to see if there are memory leaks due to the app, or due to ImageLoading, or even due to Xamarin.

molinch avatar Dec 14 '15 07:12 molinch

A bit of googling and the impression I get is the error is thrown when trying to use a bitmap that has been recycled or collected, either manually or when imageView is recycling the drawing cache for whatever reason. So perhaps it is somewhat memory related where a bitmap gets thrown out of the cache but is still being referenced somehow? The app itself is never in danger of running out of memory according to profiling.

I can more reliably trigger this during testing this morning. I need to browse around the app having lots of images being loaded, then I press the home button to minimise the app and put it to sleep. I then open the app back up via the app switcher (the square button heh, don't know the proper name!) and that's when the crash happens for me. Perhaps it's android freeing up memory and the image cache getting destroyed?

ahohns avatar Dec 15 '15 01:12 ahohns

It's not on Nuget so far but there is a branch with changes that will definitely help: https://github.com/molinch/FFImageLoading/tree/better-bitmap-pool

Could you give it a try? @goneale @ahohns

molinch avatar Dec 16 '15 12:12 molinch

@molinch Tested it for a while and didn't experience any problems yet. It works very good! The only problem is I still experience this: #91 which is definitely a cache bug (if I modify transformation key to return random string, so it bypasses cache, it works good). Could you take a look what it might be? after getting cached transformed bitmap the thread just hangs, not returning any bitmap.

daniel-luberda avatar Dec 16 '15 18:12 daniel-luberda

Ok that's great, on my side I didn't got any issue too but I tested it so far on a real projet. I'll check for #91 now

molinch avatar Dec 16 '15 19:12 molinch