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](https://github.com/facebook/react-native/commit/490e33dd8852150380ee4017c4448b3d7b436f89)

We've ported the same fix to `BaseTextInputShadowNode.h`

## Changelog:

[General] [Fixed] - Fix memory leak in TextInput

Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Mo Binni authored and meta-codesync[bot] committed 2026-02-25 03:35:14 -08:00
1 parent 677330a6d6
commit 5229e507e1
1 file changed
+13 -1
@@ -25,6 +25,16 @@
namespace facebook::react {
inline ShadowView shadowViewFromShadowNode(const ShadowNode &shadowNode)
{
auto shadowView = ShadowView{shadowNode};
// Clearing `props` and `state` (which we don't use) allows avoiding retain
// cycles.
shadowView.props = nullptr;
shadowView.state = nullptr;
return shadowView;
}
/*
* Base `ShadowNode` for <TextInput> component.
*/
@@ -171,7 +181,9 @@ class BaseTextInputShadowNode
AttributedString attributedString;
attributedString.appendFragment(
AttributedString::Fragment{
.string = props.text, .textAttributes = textAttributes, .parentShadowView = ShadowView(*this)});
.string = props.text,
.textAttributes = textAttributes,
.parentShadowView = shadowViewFromShadowNode(*this)});
auto attachments = BaseTextShadowNode::Attachments{};
BaseTextShadowNode::buildAttributedString(textAttributes, *this, attributedString, attachments);