From d79212120b7168015d3d0225ef372ed851a230fa Mon Sep 17 00:00:00 2001 From: David Vacca Date: Sat, 6 Feb 2021 23:02:02 -0800 Subject: [PATCH] Clean listeners during destroy of ReactContext Summary: This diff cleans listeners on the destruction of the ReactContext. changelog: [inernal] internal Reviewed By: JoshuaGross Differential Revision: D26259929 fbshipit-source-id: 1843cabdac2fa3e67dcc890afd923b82472d8f66 --- .../java/com/facebook/react/bridge/JSIModuleRegistry.java | 5 ++++- .../com/facebook/react/bridge/NativeModuleRegistry.java | 5 ++++- .../main/java/com/facebook/react/bridge/ReactContext.java | 6 ++++++ .../java/com/facebook/react/config/ReactFeatureFlags.java | 3 +++ .../java/com/facebook/react/uimanager/UIManagerModule.java | 7 +++++-- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSIModuleRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSIModuleRegistry.java index a4cdda0b7180c5..52e9f43f64b23f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSIModuleRegistry.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSIModuleRegistry.java @@ -8,6 +8,7 @@ package com.facebook.react.bridge; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.config.ReactFeatureFlags; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,6 +45,8 @@ public void notifyJSInstanceDestroy() { JSIModuleHolder moduleHolder = entry.getValue(); moduleHolder.notifyJSInstanceDestroy(); } - mModules.clear(); + if (ReactFeatureFlags.enableReactContextCleanupFix) { + mModules.clear(); + } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java index 9400f8e920686a..26bd8b050bea6c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModuleRegistry.java @@ -8,6 +8,7 @@ package com.facebook.react.bridge; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.config.ReactFeatureFlags; import com.facebook.react.module.annotations.ReactModule; import com.facebook.systrace.Systrace; import java.util.ArrayList; @@ -84,7 +85,9 @@ private ReactApplicationContext getReactApplicationContext() { for (ModuleHolder module : mModules.values()) { module.destroy(); } - mModules.clear(); + if (ReactFeatureFlags.enableReactContextCleanupFix) { + mModules.clear(); + } } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index 859d227243f2e8..28d024fd4b002b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -23,6 +23,7 @@ import com.facebook.react.bridge.queue.ReactQueueConfiguration; import com.facebook.react.common.LifecycleState; import com.facebook.react.common.ReactConstants; +import com.facebook.react.config.ReactFeatureFlags; import java.lang.ref.WeakReference; import java.util.concurrent.CopyOnWriteArraySet; @@ -296,6 +297,11 @@ public void destroy() { if (mCatalystInstance != null) { mCatalystInstance.destroy(); } + if (ReactFeatureFlags.enableReactContextCleanupFix) { + mLifecycleEventListeners.clear(); + mActivityEventListeners.clear(); + mWindowFocusEventListeners.clear(); + } } /** Should be called by the hosting Fragment in {@link Fragment#onActivityResult} */ diff --git a/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java b/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java index 4c520124c745ca..68afd39bb41d6e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java +++ b/ReactAndroid/src/main/java/com/facebook/react/config/ReactFeatureFlags.java @@ -62,4 +62,7 @@ public class ReactFeatureFlags { /** Enables Static ViewConfig in RN Android native code. */ public static boolean enableExperimentalStaticViewConfigs = false; + + /** Enables a more aggressive cleanup during destruction of ReactContext */ + public static boolean enableReactContextCleanupFix = false; } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index c0a22ad6b8156c..13a790fff861ec 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -247,8 +247,11 @@ public void onCatalystInstanceDestroy() { mEventDispatcher.onCatalystInstanceDestroyed(); mUIImplementation.onCatalystInstanceDestroyed(); - getReactApplicationContext().removeLifecycleEventListener(this); - getReactApplicationContext().unregisterComponentCallbacks(mMemoryTrimCallback); + ReactApplicationContext reactApplicationContext = getReactApplicationContext(); + if (ReactFeatureFlags.enableReactContextCleanupFix) { + reactApplicationContext.removeLifecycleEventListener(this); + } + reactApplicationContext.unregisterComponentCallbacks(mMemoryTrimCallback); YogaNodePool.get().clear(); ViewManagerPropertyUpdater.clear(); }