dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.28k stars 1.76k forks source link

[Android] NullReferenceException after changing background brush during StartSupportActionMode #21938

Open mfeingol opened 7 months ago

mfeingol commented 7 months ago

Description

The NRE in question:

    0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:13,5 C#
    0x22 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:357,26    C#
    0x1 in Microsoft.Maui.Graphics.Platform.GraphicsExtensions.AsColor at D:\a\_work\1\s\src\Graphics\src\Graphics\Platforms\Android\GraphicsExtensions.cs:28,4 C#
    0x1 in Microsoft.Maui.Platform.ColorExtensions.ToPlatform at D:\a\_work\1\s\src\Core\src\Platform\Android\ColorExtensions.cs:14,55  C#
    0x29 in Microsoft.Maui.Graphics.MauiDrawable.GetGradientPaintData at D:\a\_work\1\s\src\Core\src\Graphics\MauiDrawable.Android.cs:610,5 C#
    0x35 in Microsoft.Maui.Graphics.MauiDrawable.SetLinearGradientPaint at D:\a\_work\1\s\src\Core\src\Graphics\MauiDrawable.Android.cs:554,4   C#
    0xD in Microsoft.Maui.Graphics.MauiDrawable.SetPaint at D:\a\_work\1\s\src\Core\src\Graphics\MauiDrawable.Android.cs:539,5  C#
    0x3A in Microsoft.Maui.Graphics.MauiDrawable.SetBackground at D:\a\_work\1\s\src\Core\src\Graphics\MauiDrawable.Android.cs:527,6    C#
    0x180 in Microsoft.Maui.Graphics.MauiDrawable.OnDraw at D:\a\_work\1\s\src\Core\src\Graphics\MauiDrawable.Android.cs:438,6  C#
    0x24 in Android.Graphics.Drawables.ShapeDrawable.n_OnDraw_Landroid_graphics_drawable_shapes_Shape_Landroid_graphics_Canvas_Landroid_graphics_Paint_ at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Android.Graphics.Drawables.ShapeDrawable.cs:428,4  C#
    0xD in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLLL_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:356,5  C#

Steps to Reproduce

This one is complex:

  1. Set up a Grid with a LinearGradientBrush where one of the GradientStop's colors is bound to a property
  2. Set up a TouchBehavior on that grid and register for the long press event
  3. During the long press event, call Activity.StartSupportActionMode()
  4. In ActionMode.ICallback.OnCreateActionMode, change the Grid's background to a SolidColorBrush
  5. In ActionMode.ICallback.OnDestroyActionMode, set the Grid's background back to the original brush
  6. Do this twice. The second time, the NullReferenceException will be thrown

Or run the repro I provided, and:

  1. Long press on the aqua-colored "welcome" text; it will turn green.
  2. Hit back
  3. Long press again
  4. Hit back
  5. Observe the crash

Link to public reproduction project repository

https://github.com/mfeingol/repros/tree/master/BackgroundColorCrashRepro

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

14

Did you find any workaround?

No.

Relevant log output

No response

kevinxufei commented 7 months ago

Verified this issue with Visual Studio 17.10.0 Preview 4 ( 8.0.20), Can repro issue with sample project.