From 97a386c3f91e08abe948136ea9b0dab2321277b3 Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Tue, 29 Aug 2023 13:15:44 -0400 Subject: [PATCH] Fix bug when blur command received on node without XamlRoot (#12051) ## Description ### Type of Change _Erase all that don't apply._ - Bug fix (non-breaking change which fixes an issue) ### Why We are seeing crashes on quit when the blur command is called roughly around the same time that the React instance is being shut down. It's likely because the result of tryGetXamlRoot is null. ### What This change falls back on the GetFocusedElement() overload without any parameters if the XamlRoot is null, as supplying a null parameter will result in a NPE. ## Testing It's a tricky race condition to repro, but I did validate that passing a null XamlRoot value to `FocusManager::GetFocusedElement(XamlRoot)` will throw an exception. ## Changelog Should this change be included in the release notes: _yes_ Fix for NPE when blur command is received while React instance is shutting down. --- ...ative-windows-3adee0f5-93e2-4451-bc8a-967121f90520.json | 7 +++++++ vnext/Microsoft.ReactNative/Modules/NativeUIManager.cpp | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 change/react-native-windows-3adee0f5-93e2-4451-bc8a-967121f90520.json diff --git a/change/react-native-windows-3adee0f5-93e2-4451-bc8a-967121f90520.json b/change/react-native-windows-3adee0f5-93e2-4451-bc8a-967121f90520.json new file mode 100644 index 00000000000..caf3f75e930 --- /dev/null +++ b/change/react-native-windows-3adee0f5-93e2-4451-bc8a-967121f90520.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Fix bug when blur command received on node without XamlRoot", + "packageName": "react-native-windows", + "email": "erozell@outlook.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Modules/NativeUIManager.cpp b/vnext/Microsoft.ReactNative/Modules/NativeUIManager.cpp index 8b911866745..8da4aa1dea5 100644 --- a/vnext/Microsoft.ReactNative/Modules/NativeUIManager.cpp +++ b/vnext/Microsoft.ReactNative/Modules/NativeUIManager.cpp @@ -1171,7 +1171,9 @@ void NativeUIManager::blur(int64_t reactTag) { if (auto shadowNode = static_cast(m_host->FindShadowNodeForTag(reactTag))) { // Only blur if current UI is focused to avoid problem described in PR #2687 const auto xamlRoot = tryGetXamlRoot(shadowNode->m_rootTag); - if (shadowNode->GetView() == xaml::Input::FocusManager::GetFocusedElement(xamlRoot)) { + const auto focusedElement = xamlRoot ? xaml::Input::FocusManager::GetFocusedElement(xamlRoot) + : xaml::Input::FocusManager::GetFocusedElement(); + if (shadowNode->GetView() == focusedElement) { if (auto reactControl = GetParentXamlReactControl(reactTag).get()) { reactControl.as()->blur(shadowNode->GetView()); } else {