Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java_com_didi_hummer_core_engine_napi_jni_JSEngine_callFunction 内存泄漏 #447

Open
wy676579037 opened this issue Jun 26, 2024 · 1 comment
Labels
bug Something isn't working Needs: Triage 🔍

Comments

@wy676579037
Copy link

案例:
this.timer = setInterval(async () => {
console.log('每隔 1s 执行一次');
}, 1000);

每次执行JSValue都会增加一个。

extern "C"
JNIEXPORT jobject JNICALL
Java_com_didi_hummer_core_engine_napi_jni_JSEngine_callFunction(JNIEnv *env, jclass clazz, jlong js_context, jlong thisObj, jlong funcObj, jobjectArray params) {
auto globalEnv = JSUtils::toJsContext(js_context);

NAPIHandleScope handleScope;
napi_open_handle_scope(globalEnv, &handleScope);

NAPIValue jsThisObj = JSUtils::toJsValue(globalEnv, thisObj);
NAPIValue jsFuncObj = JSUtils::toJsValue(globalEnv, funcObj);
auto paramsCount = static_cast<int>(env->GetArrayLength(params));

if (jsFuncObj == nullptr) {
    return nullptr;
}

auto values = new NAPIValue[paramsCount];
for (int i = 0; i < paramsCount; i++) {
    jobject obj = env->GetObjectArrayElement(params, i);
    values[i] = JSUtils::JavaObjectToJsValue(globalEnv, obj);
    env->DeleteLocalRef(obj);
}

if (jsThisObj == nullptr) {
    jsThisObj = JSUtils::createJsUndefined(globalEnv);
}

NAPIValue result;
auto status = napi_call_function(globalEnv, jsThisObj, jsFuncObj, paramsCount, values, &result);
if (status == NAPIExceptionPendingException) {
    reportExceptionIfNeed(globalEnv);
    napi_close_handle_scope(globalEnv, handleScope);
    return nullptr;
}
delete[] values;

jobject r = JSUtils::JsValueToJavaObject(globalEnv, result, true);
napi_close_handle_scope(globalEnv, handleScope);
return r;

}
原因是每次调用 NAPIValue result; 都会返回一个Promise,导致JSUtils::JsValueToJavaObject(globalEnv, result, true)的使用做了强制+1,从而导致内存不释放

@wy676579037 wy676579037 added bug Something isn't working Needs: Triage 🔍 labels Jun 26, 2024
@wy676579037
Copy link
Author

async 会返回一个promise,普通的回调是正常的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Needs: Triage 🔍
Projects
None yet
Development

No branches or pull requests

1 participant