From 53782eafc96d9f0564e25c668c57a60321ccc2ec Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Sun, 25 Mar 2018 22:43:26 -0700 Subject: [PATCH] Fabric: Source shadow node is now stored as a weak pointer Summary: We use shource nodes only in the diffing alogorithm. It implies that we have strong pointers to those nodes in trees we compare against. Using weak_ptr's allows to avoid memory leaks. Reviewed By: mdvacca Differential Revision: D7376348 fbshipit-source-id: 34e5f58f18a00475f6bcdfbea3996b41c84dff62 --- ReactCommon/fabric/core/shadownode/ShadowNode.cpp | 9 +++++---- ReactCommon/fabric/core/shadownode/ShadowNode.h | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp index 04fb5d30f..e1394cccc 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp @@ -68,7 +68,7 @@ InstanceHandle ShadowNode::getInstanceHandle() const { } SharedShadowNode ShadowNode::getSourceNode() const { - return sourceNode_; + return sourceNode_.lock(); } void ShadowNode::sealRecursive() const { @@ -111,7 +111,7 @@ void ShadowNode::replaceChild(const SharedShadowNode &oldChild, const SharedShad void ShadowNode::clearSourceNode() { ensureUnsealed(); - sourceNode_ = nullptr; + sourceNode_.reset(); } #pragma mark - DebugStringConvertible @@ -146,10 +146,11 @@ SharedDebugStringConvertibleList ShadowNode::getDebugProps() const { list.push_back(std::make_shared("handle", std::to_string((size_t)instanceHandle_))); } - if (sourceNode_) { + SharedShadowNode sourceNode = getSourceNode(); + if (sourceNode) { list.push_back(std::make_shared( "source", - sourceNode_->getDebugDescription({.maximumDepth = 1, .format = false}) + sourceNode->getDebugDescription({.maximumDepth = 1, .format = false}) )); } diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.h b/ReactCommon/fabric/core/shadownode/ShadowNode.h index 1deb4d520..aa68556f9 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.h +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.h @@ -24,6 +24,7 @@ using SharedShadowNode = std::shared_ptr; using SharedShadowNodeList = std::vector>; using SharedShadowNodeSharedList = std::shared_ptr; using SharedShadowNodeUnsharedList = std::shared_ptr; +using WeakShadowNode = std::weak_ptr; class ShadowNode: public virtual Sealable, @@ -79,7 +80,7 @@ protected: InstanceHandle instanceHandle_; SharedProps props_; SharedShadowNodeSharedList children_; - SharedShadowNode sourceNode_; + WeakShadowNode sourceNode_; }; } // namespace react