Skip to content

Commit

Permalink
Fix memory leak by not duplicating the YGConfig
Browse files Browse the repository at this point in the history
Reviewed By: emilsjolander

Differential Revision: D6945022

fbshipit-source-id: 5fd3c3e2ac1cd94d459d5aa06e0daa8f107779ac
  • Loading branch information
priteshrnandgaonkar authored and facebook-github-bot committed Feb 9, 2018
1 parent 1a1a956 commit 400a29e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
11 changes: 3 additions & 8 deletions ReactCommon/yoga/yoga/Yoga-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
30 changes: 26 additions & 4 deletions ReactCommon/yoga/yoga/Yoga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -3642,6 +3663,7 @@ void YGNodeCalculateLayout(const YGNodeRef node,
YGPrintOptionsStyle));
}
}
YGConfigFreeRecursive(originalNode);
YGNodeFreeRecursive(originalNode);
}
}
Expand Down

0 comments on commit 400a29e

Please # to comment.