Closed fannnzhang closed 2 months ago
:warning: | Missing Reproducible Example |
---|---|
:information_source: | We could not detect a reproducible example in your issue report. Please provide either:
|
There is a memory leak in the dialog management code of React Native's new architecture. When the hide() method is called and the dialog is not currently showing, the ReactRootView is not removed from the view tree. This results in the ReactRootView holding a reference to the Activity, causing a memory leak.
Can you provide a valid repro an a screenshot from the memory profiler of Android Studio?
First, in my issue, I provided the memory leak path captured by LeakCanary. When looking at the code, this issue becomes very clear: Regardless of the state of the Dialog, mReactRootView
will always be indirectly created and held by LogModule
, and when ReactRootView
is created, it holds a reference to the Activity
. Therefore, the hide
method of LogBoxDialogSurfaceDelegate
must ensure that it is removed from the Activity
. However, the current implementation of the hide
method does not do this. When the dialog is not created or shown, it directly returns, causing the view to not release the Activity
reference, which leads to a memory leak.
To reproduce this issue is very simple. You just need to start by creating a Native Activity
in the React Native Android template project, then navigate to a ReactActivity
, and finally return to the main Activity
from the ReactActivity
. This will reliably reproduce the memory leak. Additionally, this reproduction path may also uncover other memory leaks under the new architecture. I suggest conducting similar tests as soon as possible and fixing the related issues.
My local fix code like this:
@Override
public void hide() {
if (isShowing()) {
mDialog.dismiss();
}
if (mReactRootView != null && mReactRootView.getParent() != null) {
((ViewGroup) mReactRootView.getParent()).removeView(mReactRootView);
}
mDialog = null;
}
Description
There is a memory leak in the dialog management code of React Native's new architecture. When the hide() method is called and the dialog is not currently showing, the ReactRootView is not removed from the view tree. This results in the ReactRootView holding a reference to the Activity, causing a memory leak.
Steps to reproduce
React Native Version
0.74.2
Affected Platforms
Runtime - Android
Areas
TurboModule - The New Native Module System
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/react-native-community/reproducer-react-native/tree/main
Screenshots and Videos
No response