From 400a29e15134f5264cc55b239bd2a18a107911dd Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Fri, 9 Feb 2018 04:47:49 -0800 Subject: [PATCH] Fix memory leak by not duplicating the YGConfig Reviewed By: emilsjolander Differential Revision: D6945022 fbshipit-source-id: 5fd3c3e2ac1cd94d459d5aa06e0daa8f107779ac --- ReactCommon/yoga/yoga/Yoga-internal.h | 11 +++------- ReactCommon/yoga/yoga/Yoga.cpp | 30 +++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ReactCommon/yoga/yoga/Yoga-internal.h b/ReactCommon/yoga/yoga/Yoga-internal.h index 5106d971b64025..291b3ba27b1721 100644 --- a/ReactCommon/yoga/yoga/Yoga-internal.h +++ b/ReactCommon/yoga/yoga/Yoga-internal.h @@ -68,17 +68,12 @@ struct YGCachedMeasurement { if (!std::isnan(computedWidth) || !std::isnan(measurement.computedWidth)) { isEqual = isEqual && computedWidth == measurement.computedWidth; } - if (!std::isnan( - computedHeight || !std::isnan(measurement.computedHeight))) { + if (!std::isnan(computedHeight) || + !std::isnan(measurement.computedHeight)) { isEqual = isEqual && computedHeight == measurement.computedHeight; } - return availableWidth == measurement.availableWidth && - availableHeight == measurement.availableHeight && - widthMeasureMode == measurement.widthMeasureMode && - heightMeasureMode == measurement.heightMeasureMode && - computedWidth == measurement.computedWidth && - computedHeight == measurement.computedHeight; + return isEqual; } }; diff --git a/ReactCommon/yoga/yoga/Yoga.cpp b/ReactCommon/yoga/yoga/Yoga.cpp index 6e6806f2766e33..19e8b4cf124368 100644 --- a/ReactCommon/yoga/yoga/Yoga.cpp +++ b/ReactCommon/yoga/yoga/Yoga.cpp @@ -251,6 +251,16 @@ YGNodeRef YGNodeClone(YGNodeRef oldNode) { return node; } +static YGConfigRef YGConfigClone(const YGConfig& oldConfig) { + const YGConfigRef config = new YGConfig(oldConfig); + YGAssert(config != nullptr, "Could not allocate memory for config"); + if (config == nullptr) { + abort(); + } + gConfigInstanceCount++; + return config; +} + static YGNodeRef YGNodeDeepClone(YGNodeRef oldNode) { YGNodeRef node = YGNodeClone(oldNode); YGVector vec = YGVector(); @@ -263,12 +273,12 @@ static YGNodeRef YGNodeDeepClone(YGNodeRef oldNode) { } node->setChildren(vec); - if (oldNode->getNextChild() != nullptr) { - node->setNextChild(YGNodeDeepClone(oldNode->getNextChild())); + if (oldNode->getConfig() != nullptr) { + node->setConfig(YGConfigClone(*(oldNode->getConfig()))); } - if (node->getConfig() != nullptr) { - node->setConfig(new YGConfig(*node->getConfig())); + if (oldNode->getNextChild() != nullptr) { + node->setNextChild(YGNodeDeepClone(oldNode->getNextChild())); } return node; @@ -291,6 +301,17 @@ void YGNodeFree(const YGNodeRef node) { gNodeInstanceCount--; } +static void YGConfigFreeRecursive(const YGNodeRef root) { + if (root->getConfig() != nullptr) { + gConfigInstanceCount--; + delete root->getConfig(); + } + // Delete configs recursively for childrens + for (uint32_t i = 0; i < root->getChildrenCount(); ++i) { + YGConfigFreeRecursive(root->getChild(i)); + } +} + void YGNodeFreeRecursive(const YGNodeRef root) { while (YGNodeGetChildCount(root) > 0) { const YGNodeRef child = YGNodeGetChild(root, 0); @@ -3642,6 +3663,7 @@ void YGNodeCalculateLayout(const YGNodeRef node, YGPrintOptionsStyle)); } } + YGConfigFreeRecursive(originalNode); YGNodeFreeRecursive(originalNode); } }