From 5229e507e1a1cb0cff00ab49fb21f9d47438582d Mon Sep 17 00:00:00 2001 From: Mo Binni Date: Wed, 25 Feb 2026 03:35:14 -0800 Subject: [PATCH] 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 image Reviewed By: NickGerleman, mdvacca Differential Revision: D94267721 Pulled By: javache fbshipit-source-id: 69a5b5254c2d498796c209cb133a5929d11c36ef --- .../components/textinput/BaseTextInputShadowNode.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h index 984db279df5..8fef1a5f957 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h +++ b/packages/react-native/ReactCommon/react/renderer/components/textinput/BaseTextInputShadowNode.h @@ -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 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);