From b555ac924fa33bd4640ec3291603117fc7dd013c Mon Sep 17 00:00:00 2001 From: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Date: Tue, 6 Aug 2024 14:10:04 -0700 Subject: [PATCH] [0.74] [Fabric] Custom ShadowNode's are never released (#13540) * [Fabric] Custom ShadowNodes are never released (#13536) * [Fabric] Custom ShadowNode's are never released * Change files * Update change file --- ...-47a010c1-a124-470a-8d55-8350642cbff5.json | 7 +++++ .../Fabric/AbiShadowNode.cpp | 29 ++++++++++++------- .../Fabric/AbiShadowNode.h | 2 +- .../Fabric/AbiViewShadowNode.cpp | 12 ++++---- 4 files changed, 33 insertions(+), 17 deletions(-) create mode 100644 change/react-native-windows-47a010c1-a124-470a-8d55-8350642cbff5.json diff --git a/change/react-native-windows-47a010c1-a124-470a-8d55-8350642cbff5.json b/change/react-native-windows-47a010c1-a124-470a-8d55-8350642cbff5.json new file mode 100644 index 00000000000..35f95b82498 --- /dev/null +++ b/change/react-native-windows-47a010c1-a124-470a-8d55-8350642cbff5.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "[Fabric] Custom ShadowNode's are never released", + "packageName": "react-native-windows", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.cpp b/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.cpp index a46ad6c3e1b..34ff1adb230 100644 --- a/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.cpp @@ -30,7 +30,9 @@ winrt::Microsoft::ReactNative::IComponentProps AbiProps::UserProps() const noexc ShadowNode::ShadowNode(facebook::react::ShadowNode::Shared shadowNode) noexcept : m_shadowNode(shadowNode) {} void ShadowNode::EnsureUnsealed() noexcept { - m_shadowNode->ensureUnsealed(); + if (auto shadowNode = m_shadowNode.lock()) { + shadowNode->ensureUnsealed(); + } } winrt::IInspectable ShadowNode::Tag() const noexcept { @@ -42,20 +44,25 @@ void ShadowNode::Tag(winrt::IInspectable tag) noexcept { } winrt::IInspectable ShadowNode::StateData() const noexcept { - auto state = m_shadowNode->getState(); - react_native_assert(state && "State must not be `nullptr`."); - auto abiStateData = - static_cast *>(state.get()) - ->getData(); - return abiStateData.userdata; + if (auto shadowNode = m_shadowNode.lock()) { + auto state = shadowNode->getState(); + react_native_assert(state && "State must not be `nullptr`."); + auto abiStateData = + static_cast *>(state.get()) + ->getData(); + return abiStateData.userdata; + } + return nullptr; } void ShadowNode::StateData(winrt::IInspectable tag) noexcept { - m_shadowNode->ensureUnsealed(); + if (auto shadowNode = m_shadowNode.lock()) { + shadowNode->ensureUnsealed(); - auto &state = const_cast(m_shadowNode->getState()); - state = std::make_shared>( - std::make_shared(tag), *state); + auto &state = const_cast(shadowNode->getState()); + state = std::make_shared>( + std::make_shared(tag), *state); + } } } // namespace winrt::Microsoft::ReactNative::implementation diff --git a/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.h b/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.h index 04fdc82f72b..a847f6112bb 100644 --- a/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.h +++ b/vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.h @@ -43,7 +43,7 @@ struct ShadowNode : ShadowNodeT { void StateData(winrt::IInspectable tag) noexcept; protected: - facebook::react::ShadowNode::Shared m_shadowNode; + facebook::react::ShadowNode::Weak m_shadowNode; winrt::IInspectable m_tag; }; diff --git a/vnext/Microsoft.ReactNative/Fabric/AbiViewShadowNode.cpp b/vnext/Microsoft.ReactNative/Fabric/AbiViewShadowNode.cpp index 066d81bd8f4..ec1d5a5f53e 100644 --- a/vnext/Microsoft.ReactNative/Fabric/AbiViewShadowNode.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/AbiViewShadowNode.cpp @@ -17,11 +17,13 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode(facebook::react::ShadowNode:: : base_type(shadowNode) {} void YogaLayoutableShadowNode::Layout(winrt::Microsoft::ReactNative::LayoutContext layoutContext) noexcept { - std::const_pointer_cast( - std::dynamic_pointer_cast(m_shadowNode)) - ->facebook::react::YogaLayoutableShadowNode::layout( - winrt::get_self(layoutContext) - ->m_layoutContext); + if (auto shadowNode = m_shadowNode.lock()) { + std::const_pointer_cast( + std::dynamic_pointer_cast(shadowNode)) + ->facebook::react::YogaLayoutableShadowNode::layout( + winrt::get_self(layoutContext) + ->m_layoutContext); + } } } // namespace winrt::Microsoft::ReactNative::implementation