Fix memory leak by not duplicating the YGConfig

Summary:
YGConfig isn't owned by the YGNode, and thus isn't freed when freeing the node (`YGNodeFreeRecursive`)

This fixes a memory leak caused by D6856812

Reviewed By: emilsjolander

Differential Revision: D6945022

fbshipit-source-id: 5fd3c3e2ac1cd94d459d5aa06e0daa8f107779ac
This commit is contained in:
Pritesh Nandgaonkar
2018-02-09 04:47:49 -08:00
committed by Facebook Github Bot
parent 6e38a26f32
commit 402ee11273
3 changed files with 48 additions and 12 deletions

View File

@@ -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);
}
}