Skip to content

Commit

Permalink
fix(android): use weak reference replace strong (#3975)
Browse files Browse the repository at this point in the history
Co-authored-by: maxli <maxli@tencent.com>
  • Loading branch information
siguangli and siguangli2018 committed Aug 1, 2024
1 parent cfb061b commit fc8fd5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
package com.tencent.mtt.hippy.bridge.jsi;

import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.common.ThreadExecutor.UncaughtExceptionHandler;
import com.tencent.mtt.hippy.modules.HippyModuleManagerImpl;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleBase;
import com.tencent.mtt.hippy.modules.nativemodules.HippyNativeModuleInfo;
import com.tencent.mtt.hippy.utils.LogUtils;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

public class TurboModuleManager {
public static final String TAG = "TurboModuleManager";

private final Map<String, HippyNativeModuleBase> mModuleMap = new HashMap<>();
private final HippyEngineContext mHippyEngineContext;
private final WeakReference<HippyEngineContext> mHippyEngineContextRef;

public TurboModuleManager(HippyEngineContext context) {
mHippyEngineContext = context;
mHippyEngineContextRef = new WeakReference<>(context);
}

public HippyNativeModuleBase get(String name) {
Expand All @@ -40,8 +42,11 @@ public HippyNativeModuleBase get(String name) {
if (module != null) {
return module;
}

HippyModuleManagerImpl hippyModuleManager = (HippyModuleManagerImpl) mHippyEngineContext.getModuleManager();
HippyEngineContext engineContext = mHippyEngineContextRef.get();
if (engineContext == null) {
return null;
}
HippyModuleManagerImpl hippyModuleManager = (HippyModuleManagerImpl) engineContext.getModuleManager();
HippyNativeModuleInfo moduleInfo = hippyModuleManager.getNativeModuleInfo().get(name);
if (moduleInfo == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package com.tencent.mtt.hippy.common;

import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;

public class ThreadExecutor implements Thread.UncaughtExceptionHandler {

private final HippyHandlerThread mBridgeThread;
private final HippyHandlerThread mModuleThread;
@Nullable
private UncaughtExceptionHandler mUncaughtExceptionHandler;
private WeakReference<UncaughtExceptionHandler> mUncaughtExceptionHandlerRef;
private final int mGroupId;

public ThreadExecutor(int groupId) {
Expand All @@ -35,7 +36,7 @@ public ThreadExecutor(int groupId) {
}

public void setUncaughtExceptionHandler(UncaughtExceptionHandler exceptionHandler) {
mUncaughtExceptionHandler = exceptionHandler;
mUncaughtExceptionHandlerRef = new WeakReference<>(exceptionHandler);
}

public void destroy() {
Expand All @@ -47,7 +48,6 @@ public void destroy() {
mBridgeThread.quit();
mBridgeThread.setUncaughtExceptionHandler(null);
}
mUncaughtExceptionHandler = null;
}

public HippyHandlerThread getModuleThread() {
Expand All @@ -60,8 +60,11 @@ public HippyHandlerThread getBridgeThread() {

@Override
public void uncaughtException(Thread t, Throwable e) {
if (mUncaughtExceptionHandler != null) {
mUncaughtExceptionHandler.handleThreadUncaughtException(t, e, mGroupId);
if (mUncaughtExceptionHandlerRef != null) {
UncaughtExceptionHandler handler = mUncaughtExceptionHandlerRef.get();
if (handler != null) {
handler.handleThreadUncaughtException(t, e, mGroupId);
}
} else {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit fc8fd5d

Please sign in to comment.