Skip to content

DisplayAlertAsync throws NullReferenceException when page is no longer displayed #33287

@mfeingol

Description

@mfeingol

Description

Calling DisplayAlertAsync for an unloaded page results in a NullReferenceException. I would instead expect either that the call is ignored, or the alert is displayed.

Stack trace:

12-24 15:45:30.929 E/AndroidRuntime( 7793): FATAL EXCEPTION: main
12-24 15:45:30.929 E/AndroidRuntime( 7793): Process: com.companyname.displayalertasynccrashrepro, PID: 7793
12-24 15:45:30.929 E/AndroidRuntime( 7793): android.runtime.JavaProxyThrowable: [System.NullReferenceException]: Object reference not set to an instance of an object.
12-24 15:45:30.929 E/AndroidRuntime( 7793): at Microsoft.Maui.Controls.Page.DisplayAlertAsync(//src/Controls/src/Core/Page/Page.cs:388)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at Microsoft.Maui.Controls.Page.DisplayAlertAsync(/
/src/Controls/src/Core/Page/Page.cs:357)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at DisplayAlertAsyncCrashRepro.SecondPage+d__1.MoveNext(C:\Operations\Build\Home\repros\DisplayAlertAsyncCrashRepro\DisplayAlertAsyncCrashRepro\SecondPage.xaml.cs:16)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw + 0x11(Unknown Source)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at System.Threading.Tasks.Task+<>c.b__124_0 + 0x0(Unknown Source)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at Android.App.SyncContext+<>c__DisplayClass2_0.b__0 + 0x0(Unknown Source)
12-24 15:45:30.929 E/AndroidRuntime( 7793): at Java.Lang.Thread+RunnableImplementor.Run + 0x8(Unknown Source)

Steps to Reproduce

  1. Run the repro
  2. Click on the button
  3. When the second page appears, click on the button again
  4. Observe the exception be thrown in a few seconds

Link to public reproduction project repository

https://github.com/mfeingol/repros/tree/main/DisplayAlertAsyncCrashRepro

Version with bug

10.0.20

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

Tested on Android API 36.1, but seems to repro in general

Did you find any workaround?

The code can check whether the page's IsLoaded property is enabled before calling DisplayAlertAsync. But nobody's code does that.

Relevant log output

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions