maui: Intermittent crash when navigating to a page containing images

Description

I randomly get this error when navigating to a content page that displays an image twice (uses same URL for the image but with technically using 2 different instances).

This seems to originate from the version of glide built into MAUI

Steps to Reproduce

https://github.com/brabebhin/maui-andoid-image-crash

Run the app. The first time you run it, navigate to the 2nd tab, and wait it out until it generates its 1000 images. Once the images pop up, start scrolling the list. While the list is scrolling, go to the first tab, click the button several times then go back to the second tab and scroll back up.

Repeat until the crush occurs. I’ve noticed there’s a higher chance if your phone is in landscape mode. DO try this on a phone (i tried on Samsung galaxy note 10+). Don’t know if it can be done on emulator.)

Version with bug

6.0.408 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12

Did you find any workaround?

Nope.

Relevant log output

[TabLayout] MODE_SCROLLABLE + GRAVITY_FILL is not supported, GRAVITY_START will be used instead
**Java.Lang.RuntimeException:** 'Canvas: trying to use a recycled bitmap android.graphics.Bitmap@16f3485'

[Bitmap] Called hasAlpha() on a recycle()'d bitmap! This is undefined behavior!
[Bitmap] Called getConfig() on a recycle()'d bitmap! This is undefined behavior!

Stack trace:


 	0x17 in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw	C#
 	0x8E in Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:12324,5	C#
 	0x65 in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:35,5	C#
 	0x38 in Android.Views.View.DispatchDraw at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-33/mcw/Android.Views.View.cs:14916,5	C#
 	0x11 in Microsoft.Maui.Platform.ContentViewGroup.DispatchDraw at D:\a\_work\1\s\src\Core\src\Platform\Android\ContentViewGroup.cs:41,4	C#
 	0x11 in Android.Views.View.n_DispatchDraw_Landroid_graphics_Canvas_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-33/mcw/Android.Views.View.cs:14904,4	C#
 	0x9 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:121,5	C#
 	0x3A in Java.Interop.NativeMethods.java_interop_jnienv_call_nonvirtual_boolean_method_a	C#
 	0x79 in Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualBooleanMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:11969,4	C#
 	0x65 in Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualBooleanMethod at /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:75,5	C#
 	0x70 in Android.Views.ViewGroup.DrawChild at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-33/mcw/Android.Views.ViewGroup.cs:2824,5	C#
 	0x4 in Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutRenderer.DrawChild at D:\a\_work\1\s\src\Controls\src\Core\Compatibility\Handlers\Shell\Android\ShellFlyoutRenderer.cs:171,4	C#
 	0x1C in Android.Views.ViewGroup.n_DrawChild_Landroid_graphics_Canvas_Landroid_view_View_J at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-33/mcw/Android.Views.ViewGroup.cs:2809,4	C#
 	0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLJ_Z at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:308,5	C#

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 38 (3 by maintainers)

Most upvoted comments

I have the same issue. I load a list of items from API call, the image URL points to an external URL so I’m not using FromFile at all. just trying to display items in a CollectionView. The app crashes with the same error when I scroll a little bit.

I spent a few hours to analyze this problem.

This bug was created following this pull request: https://github.com/dotnet/maui/pull/7348

While waiting for Microsoft to permanently fix this bug, the workaround consists of redoing the clear before each source image change.

Below is an example of code to add the clear in the source image mapping.

This workaround is applicable to the following bug https://github.com/dotnet/maui/issues/11519 https://github.com/dotnet/maui/issues/11130 https://github.com/dotnet/maui/issues/10032 https://github.com/dotnet/maui/issues/9712 https://github.com/dotnet/maui/issues/8809 https://github.com/dotnet/maui/issues/9011 https://github.com/dotnet/maui/issues/8178 https://github.com/dotnet/maui/issues/8809

using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

namespace MauiCanvas;

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
				fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
			});

#if __ANDROID__
        ImageHandler.Mapper.PrependToMapping(nameof(Microsoft.Maui.IImage.Source), (handler, view) => PrependToMappingImageSource(handler, view));
#endif

        return builder.Build();
	}

#if __ANDROID__
    public static void PrependToMappingImageSource(IImageHandler handler, Microsoft.Maui.IImage image)
    {
        handler.PlatformView?.Clear();
    }
#endif

}

This is a blocking issue on Android. Incredibly easy to reproduce: a collection view with multiple images loaded from a Uri will 100% crash the app every single time.

I had the same problem again. Using an image with only one Size-Request (i.e. HeightRequest) and no aspect fit results in random errors. When I set both: height- and widthrequest the error is gone. Doesn´t matter which kind of imagesource (uri, file etc.).