Skip to content

Commit

Permalink
Merge pull request #80 from HarlonWang/feature/2.3.0
Browse files Browse the repository at this point in the history
Feature/2.3.0
  • Loading branch information
HarlonWang authored Oct 9, 2024
2 parents 9a586e9 + 93ec930 commit c4912a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion native/cpp/quickjs_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ static char *jsModuleNormalizeFunc(JSContext *ctx, const char *module_base_name,
env->DeleteLocalRef(moduleLoader);

// todo 这里作为返回值,没有调用 ReleaseStringUTFChars,quickjs.c 里面会对 char* 进行释放,需要 check 下是否有释放?
return (char *) env->GetStringUTFChars((jstring) result, nullptr);
auto ret = (char *) env->GetStringUTFChars((jstring) result, nullptr);
env->DeleteLocalRef(result);
return ret;
}

static JSModuleDef *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void initSo() {
public static QuickJSContext createContext() {
QuickJSContext context = QuickJSContext.create();
context.setConsole(new LogcatConsole("console-test"));
context.setLeakDetectionListener((leak, stringValue) -> Log.e("leak-object", stringValue));
return context;
}

Expand Down Expand Up @@ -1222,4 +1221,19 @@ public void testObjectToMapFilter() {
context.destroy();
}

@Test
public void testObjectLeakDetection() {
QuickJSContext context = createContext();
context.setLeakDetectionListener((leak, stringValue) -> {
assertEquals(stringValue, "{ name: 'leak1' }");
});

// 泄漏场景
JSObject o = context.createNewJSObject();
o.setProperty("name", "leak1");
// o.release();

context.destroy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,20 @@ public void releaseObjectRecords() {
}

public void releaseObjectRecords(boolean needRelease) {
// 检测是否有未被释放引用的对象,如果有的话,根据计数释放一下
JSFunction format = getGlobalObject().getJSFunction("format");

// 检测是否有未被释放引用的对象,如果有的话,根据计数释放一下
Iterator<JSObject> objectIterator = objectRecords.iterator();
while (objectIterator.hasNext()) {
JSObject object = objectIterator.next();
// 全局对象交由引擎层会回收,这里先过滤掉
if (!object.isRefCountZero() && object != getGlobalObject()) {

// 这里需要过滤掉 getGlobalObject 和 format
// 1. getGlobalObject 全局对象不会主动释放,引擎销毁会回收
// 2. format 用来格式化内容,会在迭代完释放掉,这里过滤掉
if (!object.isRefCountZero() && object != getGlobalObject() && object != format) {
int refCount = object.getRefCount();
if (leakDetectionListener != null) {
String value = null;
if (format != null) {
value = (String) format.call(object);
}
String value = (String) format.call(object);
leakDetectionListener.notifyLeakDetected(object, value);
}

Expand All @@ -320,6 +321,8 @@ public void releaseObjectRecords(boolean needRelease) {
}
}
}

format.release();
}

public List<JSObject> getObjectRecords() {
Expand Down

0 comments on commit c4912a8

Please sign in to comment.