diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp index 438d5fbb9a61..44cca7436b98 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp @@ -310,7 +310,8 @@ bool ShadowNode::progressStateIfNecessary() { } void ShadowNode::setRuntimeShadowNodeReference( - ShadowNodeWrapper* runtimeShadowNodeReference) const { + const std::shared_ptr& runtimeShadowNodeReference) + const { runtimeShadowNodeReference_ = runtimeShadowNodeReference; } @@ -319,8 +320,8 @@ void ShadowNode::transferRuntimeShadowNodeReference( destinationShadowNode->runtimeShadowNodeReference_ = runtimeShadowNodeReference_; - if (runtimeShadowNodeReference_ != nullptr) { - runtimeShadowNodeReference_->shadowNode = destinationShadowNode; + if (auto reference = runtimeShadowNodeReference_.lock()) { + reference->shadowNode = destinationShadowNode; } } diff --git a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h index ea5dae23eebd..48196b4475b0 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ShadowNode.h @@ -190,11 +190,11 @@ class ShadowNode : public Sealable, bool progressStateIfNecessary(); /* - * Bind the runtime reference to this `ShadowNode` with a raw pointer, + * Bind the runtime reference to this `ShadowNode` with a weak pointer, * allowing to update the reference to this `ShadowNode` when cloned. */ - void setRuntimeShadowNodeReference( - ShadowNodeWrapper* runtimeShadowNodeReference) const; + void setRuntimeShadowNodeReference(const std::shared_ptr& + runtimeShadowNodeReference) const; /* * Transfer the runtime reference to this `ShadowNode` to a new instance, @@ -269,9 +269,9 @@ class ShadowNode : public Sealable, ShadowNodeTraits traits_; /* - * Pointer to the runtime reference to this `ShadowNode`. + * Weak pointer to the runtime reference to this `ShadowNode`. */ - mutable ShadowNodeWrapper* runtimeShadowNodeReference_{}; + mutable std::weak_ptr runtimeShadowNodeReference_{}; }; static_assert( diff --git a/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp b/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp index 4a33373969d5..1430eb027dfb 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/core/tests/ShadowNodeTest.cpp @@ -353,7 +353,7 @@ TEST_P(ShadowNodeTest, testCloneTree) { TEST_P(ShadowNodeTest, handleRuntimeReferenceTransferOnClone) { auto nodeABRev1 = nodeAB_->clone({}); auto wrappedShadowNode = std::make_shared(nodeABRev1); - nodeABRev1->setRuntimeShadowNodeReference(&*wrappedShadowNode); + nodeABRev1->setRuntimeShadowNodeReference(wrappedShadowNode); auto nodeABRev2 = nodeABRev1->clone({}); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h b/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h index 0a6b29dab582..b88b288c99bb 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/primitives.h @@ -52,7 +52,7 @@ inline static jsi::Value valueFromShadowNode( if (assignRuntimeShadowNodeReference) { wrappedShadowNode->shadowNode->setRuntimeShadowNodeReference( - &*wrappedShadowNode); + wrappedShadowNode); } jsi::Object obj(runtime);