Fix crash when a non-leaf node becomes a leaf node

Summary:
When a non-leaf node becomes a leaf node, `YGAttachNodesFromViewHierachy` calls `YGNodeSetMeasureFunc` before `YGRemoveAllChildren`, which causes a crash because that node has children. Fixed by swapping the order of the calls and have a unit-test to make sure it works.
Closes https://github.com/facebook/yoga/pull/320

Differential Revision: D4383905

Pulled By: dshahidehpour

fbshipit-source-id: 4872b1e819f178adaaa66b1f1c233390d3ec3be9
This commit is contained in:
David Hart
2017-01-05 08:16:33 -08:00
committed by Facebook Github Bot
parent bf6602ebff
commit 1877a97898
2 changed files with 19 additions and 1 deletions

View File

@@ -343,4 +343,22 @@
}
}
- (void)testThatANonLeafNodeCanBecomeALeafNode
{
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[container yg_setUsesYoga:YES];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectZero];
[subview1 yg_setUsesYoga:YES];
[container addSubview:subview1];
UIView *subview2 = [[UIView alloc] initWithFrame:CGRectZero];
[subview2 yg_setUsesYoga:YES];
[subview1 addSubview:subview2];
[container yg_applyLayout];
[subview2 removeFromSuperview];
[container yg_applyLayout];
}
@end

View File

@@ -316,8 +316,8 @@ static void YGAttachNodesFromViewHierachy(UIView *const view)
// Only leaf nodes should have a measure function
if (view.yg_isLeaf) {
YGNodeSetMeasureFunc(node, YGMeasureView);
YGRemoveAllChildren(node);
YGNodeSetMeasureFunc(node, YGMeasureView);
} else {
YGNodeSetMeasureFunc(node, NULL);