Skip to content

Commit 5229e50

Browse files
mobinnimeta-codesync[bot]
authored andcommitted
Fix memory leak in TextInput (#55719)
Summary: When implementing a local FPS monitor I noticed that `TextInput` leaked memory. tomekzaw helped triage and confirm whether the leak was occurring in reanimated or react-native upstream. We collaborated on a fix for this and noticed that this was fixed 6 years ago in text [here](490e33d) We've ported the same fix to `BaseTextInputShadowNode.h` ## Changelog: [General] [Fixed] - Fix memory leak in TextInput Pull Request resolved: #55719 Test Plan: https://snack.expo.dev/mobinni/memory-leak-reproducer - Run this with XCode instruments and see memory grow over time <img width="1094" height="930" alt="image" src="https://github.com/user-attachments/assets/bc9abb5a-42ff-430c-8efb-a418ba65ddc2" /> Reviewed By: NickGerleman, mdvacca Differential Revision: D94267721 Pulled By: javache fbshipit-source-id: 69a5b5254c2d498796c209cb133a5929d11c36ef
1 parent 677330a commit 5229e50

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525

2626
namespace facebook::react {
2727

28+
inline ShadowView shadowViewFromShadowNode(const ShadowNode &shadowNode)
29+
{
30+
auto shadowView = ShadowView{shadowNode};
31+
// Clearing `props` and `state` (which we don't use) allows avoiding retain
32+
// cycles.
33+
shadowView.props = nullptr;
34+
shadowView.state = nullptr;
35+
return shadowView;
36+
}
37+
2838
/*
2939
* Base `ShadowNode` for <TextInput> component.
3040
*/
@@ -171,7 +181,9 @@ class BaseTextInputShadowNode
171181
AttributedString attributedString;
172182
attributedString.appendFragment(
173183
AttributedString::Fragment{
174-
.string = props.text, .textAttributes = textAttributes, .parentShadowView = ShadowView(*this)});
184+
.string = props.text,
185+
.textAttributes = textAttributes,
186+
.parentShadowView = shadowViewFromShadowNode(*this)});
175187

176188
auto attachments = BaseTextShadowNode::Attachments{};
177189
BaseTextShadowNode::buildAttributedString(textAttributes, *this, attributedString, attachments);

0 commit comments

Comments
 (0)