Skip to content

Commit

Permalink
Clean listeners during destroy of ReactContext
Browse files Browse the repository at this point in the history
Summary:
This diff cleans listeners on the destruction of the ReactContext.

changelog: [inernal] internal

Reviewed By: JoshuaGross

Differential Revision: D26259929

fbshipit-source-id: 1843cabdac2fa3e67dcc890afd923b82472d8f66
  • Loading branch information
mdvacca authored and facebook-github-bot committed Feb 7, 2021
1 parent 98165a2 commit d792121
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +45,8 @@ public void notifyJSInstanceDestroy() {
JSIModuleHolder moduleHolder = entry.getValue();
moduleHolder.notifyJSInstanceDestroy();
}
mModules.clear();
if (ReactFeatureFlags.enableReactContextCleanupFix) {
mModules.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit d792121

Please sign in to comment.