Skip to content

Commit

Permalink
Fixes issue where Alert crashes (#11168)
Browse files Browse the repository at this point in the history
* Fixes issue where Alert crashes

There are edge cases where an Alert may be shown while the app is
exiting. Generally, when the app is exiting, apps will have set the
XamlRoot instance on XamlUIService to null. In this case, we may still
attempt to show the alert on a XAML Island with a null XamlRoot
instance, which would cause a crash.

This change just ensures that, if we are in a XAML Island and we don't
have a valid XamlRoot to attach to the ContentDialog, we just treat the
alert as dismissed.

* Change files
  • Loading branch information
rozele committed Mar 31, 2023
1 parent fecd9a4 commit 340db73
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Fixes issue where Alert crashes",
"packageName": "react-native-windows",
"email": "erozell@outlook.com",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions vnext/Microsoft.ReactNative/Modules/AlertModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ void Alert::ProcessPendingAlertRequestsXaml() noexcept {
});

dialog.Closed([=](auto &&, auto &&) { xamlRoot.Changed(rootChangedToken); });
} else if (IsXamlIsland()) {
// We cannot show a ContentDialog in a XAML Island unless it is assigned a
// XamlRoot instance. In such cases, we just treat the alert as dismissed.
jsDispatcher.Post([result, this] { result(m_constants.dismissed, m_constants.buttonNeutral); });
pendingAlerts.pop();
ProcessPendingAlertRequests();
return;
}
}

Expand Down

0 comments on commit 340db73

Please sign in to comment.