Skip to content

Commit fb07ea3

Browse files
Fix TextInput state retain chain in AndroidTextInputShadowNode
Summary: `AndroidTextInputShadowNode::getAttributedString` builds the fragment for a controlled `value` with `parentShadowView = ShadowView(*this)`. A full `ShadowView` carries the node's current `state` and `props`. `updateStateIfNeeded` stores that attributed string in the next `AndroidTextInputState` (`reactTreeAttributedString`), so every new state keeps the previous state (and props) alive. The chain grows by one link per value change for as long as the input stays mounted. Two effects: - Memory: native heap grows with every change to a controlled `TextInput` and is only released on unmount. - Crash: on unmount the whole chain is destroyed recursively on the JS thread. Long chains overflow the stack, and crash stacks repeat `State → AttributedString → Fragment → ShadowView → State` dozens of times before ending in a SIGSEGV in `~AndroidTextInputState()`. D94267721 fixed the same retain chain for iOS/macOS by adding `shadowViewFromShadowNode()` in `BaseTextInputShadowNode.h`. `AndroidTextInputShadowNode` does not inherit from `BaseTextInputShadowNode`, so Android still had the bug. This change clears `props` and `state` on the parent `ShadowView` before storing it in the fragment, mirroring the iOS fix. On Android, `parentShadowView` is only read for `tag`, `componentHandle` and `layoutMetrics`. Its `state` and `props` are never read. ## Changelog: [Android] [Fixed] - Fix memory leak in TextInput: each state retained the previous one, which could overflow the stack on unmount Differential Revision: D131297146 Pull Request resolved: #59980
1 parent 2387ec5 commit fb07ea3

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

  • packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ AttributedString AndroidTextInputShadowNode::getAttributedString(
189189
// text value's background will stack. This is a hack/workaround to prevent
190190
// that effect.
191191
fragment.textAttributes.backgroundColor = clearColor();
192-
fragment.parentShadowView = ShadowView(*this);
192+
auto parentShadowView = ShadowView(*this);
193+
// Clearing `props` and `state` (which we don't use) allows avoiding retain
194+
// cycles.
195+
parentShadowView.props = nullptr;
196+
parentShadowView.state = nullptr;
197+
fragment.parentShadowView = std::move(parentShadowView);
193198
attributedString.prependFragment(std::move(fragment));
194199
}
195200

0 commit comments

Comments
 (0)