From cd024e08abf84784612291fbdda83ddaba168924 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 21 Feb 2023 11:46:45 +0800 Subject: [PATCH] mem leak Signed-off-by: zuojiangjiang --- .../distributeddata/src/napi_queue.cpp | 16 +++++++++++----- .../jskitsimpl/distributeddata/src/uv_queue.cpp | 8 ++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp b/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp index fa51b890..591cca07 100644 --- a/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp @@ -28,7 +28,9 @@ ContextBase::~ContextBase() if (callbackRef != nullptr) { napi_delete_reference(env, callbackRef); } - napi_delete_reference(env, selfRef); + if (selfRef != nullptr) { + napi_delete_reference(env, selfRef); + } env = nullptr; } } @@ -42,7 +44,9 @@ void ContextBase::GetCbInfo(napi_env envi, napi_callback_info info, NapiCbInfoPa CHECK_STATUS(this, "napi_get_cb_info failed!"); CHECK_ARGS(this, argc <= ARGC_MAX, "too many arguments!"); CHECK_ARGS(this, self != nullptr, "no JavaScript this argument!"); - napi_create_reference(env, self, 1, &selfRef); + if (!sync) { + napi_create_reference(env, self, 1, &selfRef); + } status = napi_unwrap(env, self, &native); CHECK_STATUS(this, "self unwrap failed!"); @@ -72,8 +76,6 @@ napi_value NapiQueue::AsyncWork(napi_env env, std::shared_ptr ctxt, NapiAsyncExecute execute, NapiAsyncComplete complete) { ZLOGD("name=%{public}s", name.c_str()); - ctxt->execute = std::move(execute); - ctxt->complete = std::move(complete); napi_value promise = nullptr; if (ctxt->callbackRef == nullptr) { @@ -108,8 +110,10 @@ napi_value NapiQueue::AsyncWork(napi_env env, std::shared_ptr ctxt, GenerateOutput(ctxt); }, (void*)(ctxt.get()), &ctxt->work); - napi_queue_async_work(ctxt->env, ctxt->work); + ctxt->execute = std::move(execute); + ctxt->complete = std::move(complete); ctxt->hold = ctxt; // save crossing-thread ctxt. + napi_queue_async_work(ctxt->env, ctxt->work); return promise; } @@ -143,6 +147,8 @@ void NapiQueue::GenerateOutput(ContextBase* ctxt) ZLOGD("call callback function"); napi_call_function(ctxt->env, nullptr, callback, RESULT_ALL, result, &callbackResult); } + ctxt->execute = nullptr; + ctxt->complete = nullptr; ctxt->hold.reset(); // release ctxt. } } // namespace OHOS::DistributedData diff --git a/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp b/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp index 8a58d08c..66eaf517 100644 --- a/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp +++ b/frameworks/jskitsimpl/distributeddata/src/uv_queue.cpp @@ -53,9 +53,14 @@ void UvQueue::AsyncCall(NapiCallbackGetter getter, NapiArgsGenerator genArgs) delete data; delete work; }); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(entry->env, &scope); napi_value method = entry->callback(entry->env); if (method == nullptr) { ZLOGE("the callback is invalid, maybe is cleared!"); + if (scope != nullptr) { + napi_close_handle_scope(entry->env, scope); + } return ; } int argc = 0; @@ -72,6 +77,9 @@ void UvQueue::AsyncCall(NapiCallbackGetter getter, NapiArgsGenerator genArgs) if (status != napi_ok) { ZLOGE("notify data change failed status:%{public}d callback:%{public}p", status, method); } + if (scope != nullptr) { + napi_close_handle_scope(entry->env, scope); + } }); } -- Gitee