Skip to content

Commit 7eb389a

Browse files
sherginosdnk
authored andcommitted
Fabric: Fixed memory leak caused by retain cycle in BaseTextShadowNode
Summary: Clearing `props` and `state` (which we don't use) allows avoiding retain cycles. The memory leak was caused by a retain cycle: AttributedString -> Fragment -> ShadowView -> PharagraphState -> TextLayoutManager -> Cache -> AttributedString. We don't use `props` and `state` parts of `ShadowView` inside `AttributedString`. Changelog: [Internal] Fabric-specific internal change. Reviewed By: mdvacca Differential Revision: D20182791 fbshipit-source-id: 2ddc5d53a1cb594e3d1cc39933e8958eb6425389
1 parent f7dd4a4 commit 7eb389a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ReactCommon/fabric/components/text/basetext/BaseTextShadowNode.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
namespace facebook {
1818
namespace react {
1919

20+
inline ShadowView shadowViewFromShadowNode(ShadowNode const &shadowNode) {
21+
auto shadowView = ShadowView{shadowNode};
22+
// Clearing `props` and `state` (which we don't use) allows avoiding retain
23+
// cycles.
24+
shadowView.props = nullptr;
25+
shadowView.state = nullptr;
26+
return shadowView;
27+
}
28+
2029
AttributedString BaseTextShadowNode::getAttributedString(
2130
TextAttributes const &textAttributes,
2231
ShadowNode const &parentNode) {
@@ -35,7 +44,7 @@ AttributedString BaseTextShadowNode::getAttributedString(
3544
// `attributedString` causes a retain cycle (besides that fact that we
3645
// don't need it at all). Storing a `ShadowView` instance instead of
3746
// `ShadowNode` should properly fix this problem.
38-
fragment.parentShadowView = ShadowView(parentNode);
47+
fragment.parentShadowView = shadowViewFromShadowNode(parentNode);
3948
attributedString.appendFragment(fragment);
4049
continue;
4150
}
@@ -56,7 +65,7 @@ AttributedString BaseTextShadowNode::getAttributedString(
5665
// Any other kind of ShadowNode
5766
auto fragment = AttributedString::Fragment{};
5867
fragment.string = AttributedString::Fragment::AttachmentCharacter();
59-
fragment.parentShadowView = ShadowView(*childNode);
68+
fragment.parentShadowView = shadowViewFromShadowNode(*childNode);
6069
fragment.textAttributes = textAttributes;
6170
attributedString.appendFragment(fragment);
6271
}

0 commit comments

Comments
 (0)