From 1877a97898e6af647ccc8fbe298c0cdd5620913b Mon Sep 17 00:00:00 2001 From: David Hart Date: Thu, 5 Jan 2017 08:16:33 -0800 Subject: [PATCH] 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 --- YogaKit/Tests/YogaKitTests.m | 18 ++++++++++++++++++ YogaKit/UIView+Yoga.m | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/YogaKit/Tests/YogaKitTests.m b/YogaKit/Tests/YogaKitTests.m index 68d97dd5..e4de239a 100644 --- a/YogaKit/Tests/YogaKitTests.m +++ b/YogaKit/Tests/YogaKitTests.m @@ -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 diff --git a/YogaKit/UIView+Yoga.m b/YogaKit/UIView+Yoga.m index 3c6267ee..9774d3e9 100644 --- a/YogaKit/UIView+Yoga.m +++ b/YogaKit/UIView+Yoga.m @@ -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);