diff --git a/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp b/frameworks/jskitsimpl/distributeddata/src/napi_queue.cpp index fa51b8906d69eed27621074a2d19db55f8df567c..591cca0753ada759f76368c656332c0e19827db8 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 8a58d08c09829f2c6dad39c831f46268889a77d5..66eaf517dfd817da3417b258d51c22a3a5cc78ca 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); + } }); }