Skip to content

Commit

Permalink
[Fabric] Custom ShadowNodes are never released (#13536) (#13539)
Browse files Browse the repository at this point in the history
* [Fabric] Custom ShadowNode's are never released

* Change files
  • Loading branch information
acoates-ms committed Aug 6, 2024
1 parent 433c4d2 commit 2a1e56f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "[Fabric] Custom ShadowNode's are never released",
"packageName": "react-native-windows",
"email": "30809111+acoates-ms@users.noreply.github.com",
"dependentChangeType": "patch"
}
29 changes: 18 additions & 11 deletions vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<const facebook::react::ConcreteState<::Microsoft::ReactNative::AbiStateData> *>(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<const facebook::react::ConcreteState<::Microsoft::ReactNative::AbiStateData> *>(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<facebook::react::State::Shared &>(m_shadowNode->getState());
state = std::make_shared<const facebook::react::ConcreteState<::Microsoft::ReactNative::AbiStateData>>(
std::make_shared<const ::Microsoft::ReactNative::AbiStateData>(tag), *state);
auto &state = const_cast<facebook::react::State::Shared &>(shadowNode->getState());
state = std::make_shared<const facebook::react::ConcreteState<::Microsoft::ReactNative::AbiStateData>>(
std::make_shared<const ::Microsoft::ReactNative::AbiStateData>(tag), *state);
}
}

} // namespace winrt::Microsoft::ReactNative::implementation
Expand Down
2 changes: 1 addition & 1 deletion vnext/Microsoft.ReactNative/Fabric/AbiShadowNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct ShadowNode : ShadowNodeT<ShadowNode> {
void StateData(winrt::IInspectable tag) noexcept;

protected:
facebook::react::ShadowNode::Shared m_shadowNode;
facebook::react::ShadowNode::Weak m_shadowNode;
winrt::IInspectable m_tag;
};

Expand Down
12 changes: 7 additions & 5 deletions vnext/Microsoft.ReactNative/Fabric/AbiViewShadowNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<facebook::react::YogaLayoutableShadowNode>(
std::dynamic_pointer_cast<const facebook::react::YogaLayoutableShadowNode>(m_shadowNode))
->facebook::react::YogaLayoutableShadowNode::layout(
winrt::get_self<winrt::Microsoft::ReactNative::implementation::LayoutContext>(layoutContext)
->m_layoutContext);
if (auto shadowNode = m_shadowNode.lock()) {
std::const_pointer_cast<facebook::react::YogaLayoutableShadowNode>(
std::dynamic_pointer_cast<const facebook::react::YogaLayoutableShadowNode>(shadowNode))
->facebook::react::YogaLayoutableShadowNode::layout(
winrt::get_self<winrt::Microsoft::ReactNative::implementation::LayoutContext>(layoutContext)
->m_layoutContext);
}
}

} // namespace winrt::Microsoft::ReactNative::implementation
Expand Down

0 comments on commit 2a1e56f

Please sign in to comment.