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
This commit is contained in:
Valentin Shergin 2018-03-25 22:43:26 -07:00 committed by Facebook Github Bot
parent ca20fcd47d
commit 53782eafc9
2 changed files with 7 additions and 5 deletions

View File

@ -68,7 +68,7 @@ InstanceHandle ShadowNode::getInstanceHandle() const {
} }
SharedShadowNode ShadowNode::getSourceNode() const { SharedShadowNode ShadowNode::getSourceNode() const {
return sourceNode_; return sourceNode_.lock();
} }
void ShadowNode::sealRecursive() const { void ShadowNode::sealRecursive() const {
@ -111,7 +111,7 @@ void ShadowNode::replaceChild(const SharedShadowNode &oldChild, const SharedShad
void ShadowNode::clearSourceNode() { void ShadowNode::clearSourceNode() {
ensureUnsealed(); ensureUnsealed();
sourceNode_ = nullptr; sourceNode_.reset();
} }
#pragma mark - DebugStringConvertible #pragma mark - DebugStringConvertible
@ -146,10 +146,11 @@ SharedDebugStringConvertibleList ShadowNode::getDebugProps() const {
list.push_back(std::make_shared<DebugStringConvertibleItem>("handle", std::to_string((size_t)instanceHandle_))); list.push_back(std::make_shared<DebugStringConvertibleItem>("handle", std::to_string((size_t)instanceHandle_)));
} }
if (sourceNode_) { SharedShadowNode sourceNode = getSourceNode();
if (sourceNode) {
list.push_back(std::make_shared<DebugStringConvertibleItem>( list.push_back(std::make_shared<DebugStringConvertibleItem>(
"source", "source",
sourceNode_->getDebugDescription({.maximumDepth = 1, .format = false}) sourceNode->getDebugDescription({.maximumDepth = 1, .format = false})
)); ));
} }

View File

@ -24,6 +24,7 @@ using SharedShadowNode = std::shared_ptr<const ShadowNode>;
using SharedShadowNodeList = std::vector<std::shared_ptr<const ShadowNode>>; using SharedShadowNodeList = std::vector<std::shared_ptr<const ShadowNode>>;
using SharedShadowNodeSharedList = std::shared_ptr<const SharedShadowNodeList>; using SharedShadowNodeSharedList = std::shared_ptr<const SharedShadowNodeList>;
using SharedShadowNodeUnsharedList = std::shared_ptr<SharedShadowNodeList>; using SharedShadowNodeUnsharedList = std::shared_ptr<SharedShadowNodeList>;
using WeakShadowNode = std::weak_ptr<const ShadowNode>;
class ShadowNode: class ShadowNode:
public virtual Sealable, public virtual Sealable,
@ -79,7 +80,7 @@ protected:
InstanceHandle instanceHandle_; InstanceHandle instanceHandle_;
SharedProps props_; SharedProps props_;
SharedShadowNodeSharedList children_; SharedShadowNodeSharedList children_;
SharedShadowNode sourceNode_; WeakShadowNode sourceNode_;
}; };
} // namespace react } // namespace react