feat:fix iOS memory leak in debugging

This commit is contained in:
pengfei.zhou
2021-03-04 10:02:29 +08:00
committed by osborn
parent b972cce1cd
commit 627f323ae3
14 changed files with 124 additions and 36 deletions

View File

@@ -41,14 +41,12 @@ public class DoricDebugDriver implements IDoricDriver {
private final DoricDebugJSEngine doricDebugJSEngine;
private final ExecutorService mBridgeExecutor;
private final Handler mUIHandler;
private final Handler mJSHandler;
private String theContextId = null;
public DoricDebugDriver(WSClient wsClient) {
doricDebugJSEngine = new DoricDebugJSEngine(wsClient);
mBridgeExecutor = Executors.newCachedThreadPool();
mUIHandler = new Handler(Looper.getMainLooper());
mJSHandler = doricDebugJSEngine.getJSHandler();
}
@Override
@@ -64,7 +62,7 @@ public class DoricDebugDriver implements IDoricDriver {
@Override
public AsyncResult<JSDecoder> invokeDoricMethod(final String method, final Object... args) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<JSDecoder>() {
return AsyncCall.ensureRunInExecutor(mBridgeExecutor, new Callable<JSDecoder>() {
@Override
public JSDecoder call() {
try {
@@ -80,11 +78,10 @@ public class DoricDebugDriver implements IDoricDriver {
@Override
public <T> AsyncResult<T> asyncCall(Callable<T> callable, ThreadMode threadMode) {
switch (threadMode) {
case JS:
return AsyncCall.ensureRunInHandler(mJSHandler, callable);
case UI:
return AsyncCall.ensureRunInHandler(mUIHandler, callable);
case INDEPENDENT:
case JS:
default:
return AsyncCall.ensureRunInExecutor(mBridgeExecutor, callable);
}
@@ -92,7 +89,7 @@ public class DoricDebugDriver implements IDoricDriver {
@Override
public AsyncResult<Boolean> createContext(final String contextId, final String script, final String source) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
return AsyncCall.ensureRunInExecutor(mBridgeExecutor, new Callable<Boolean>() {
@Override
public Boolean call() {
try {
@@ -108,7 +105,7 @@ public class DoricDebugDriver implements IDoricDriver {
@Override
public AsyncResult<Boolean> destroyContext(final String contextId) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
return AsyncCall.ensureRunInExecutor(mBridgeExecutor, new Callable<Boolean>() {
@Override
public Boolean call() {
try {

View File

@@ -11,16 +11,21 @@ import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;
import pub.doric.devkit.WSClient;
public class RemoteJSExecutor implements WSClient.Interceptor {
private final Map<String, JavaFunction> globalFunctions = new HashMap<>();
private JSDecoder temp;
private final WSClient wsClient;
private final Thread currentThread;
private final AtomicInteger callIdCounter = new AtomicInteger();
private Map<Integer, Thread> mThreads = new HashMap<>();
private Map<Integer, JSDecoder> mResults = new HashMap<>();
public RemoteJSExecutor(WSClient wsClient) {
this.wsClient = wsClient;
this.wsClient.addInterceptor(this);
@@ -63,6 +68,7 @@ public class RemoteJSExecutor implements WSClient.Interceptor {
.put("value", javaValue.getValue())
.toJSONObject());
}
int callId = callIdCounter.incrementAndGet();
wsClient.sendToDebugger(
"invokeMethod",
new JSONBuilder()
@@ -70,11 +76,13 @@ public class RemoteJSExecutor implements WSClient.Interceptor {
.put("objectName", objectName)
.put("functionName", functionName)
.put("values", jsonArray)
.put("callId", callId)
.put("hashKey", hashKey)
.toJSONObject());
LockSupport.park(Thread.currentThread());
return temp;
Thread thread = Thread.currentThread();
mThreads.put(callId, thread);
LockSupport.park(thread);
return mResults.remove(callId);
}
public void destroy() {
@@ -99,15 +107,16 @@ public class RemoteJSExecutor implements WSClient.Interceptor {
}
break;
case "invokeMethod": {
int callId = payload.optInt("callId");
try {
Object result = payload.opt("result");
ValueBuilder vb = new ValueBuilder(result);
temp = new JSDecoder(vb.build());
mResults.put(callId, new JSDecoder(vb.build()));
System.out.println(result);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
LockSupport.unpark(currentThread);
LockSupport.unpark(mThreads.remove(callId));
}
}
break;