Permalink
Browse files

Fix memory leak by not duplicating the YGConfig

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 400a29e15134f5264cc55b239bd2a18a107911dd
Showing with 29 additions and 12 deletions.
  1. +3 −8 ReactCommon/yoga/yoga/Yoga-internal.h
  2. +26 −4 ReactCommon/yoga/yoga/Yoga.cpp
@@ -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;
}
};
@@ -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);
}
}

0 comments on commit 400a29e

Please sign in to comment.