From c5960c6127cf6318afc56cfe95ef40e94e1f9c7e Mon Sep 17 00:00:00 2001 From: fushuchang Date: Sat, 25 Feb 2023 10:51:27 +0800 Subject: [PATCH] fixed 154ecd5 from https://gitee.com/cnhesai/filemanagement_user_file_service/pulls/364 use handle_scope management napi_value lifecycle Signed-off-by: fushuchang --- .../napi/file_access_module/napi_notify_callback.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp b/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp index 7e01fdde..4f45813e 100644 --- a/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp +++ b/interfaces/kits/napi/file_access_module/napi_notify_callback.cpp @@ -57,6 +57,14 @@ int NapiNotifyCallback::OnNotify(const NotifyMessage& message) [](uv_work_t *work) {}, [](uv_work_t *work, int status) { CallbackParam *param = reinterpret_cast(work->data); + napi_handle_scope scope = nullptr; + // 打开handle scope用于管理napi_value的生命周期,否则会内存泄漏 + napi_open_handle_scope(param->callback_->env_, &scope); + if (scope == nullptr) { + HILOG_ERROR("napi_open_handle_scope failed"); + return; + } + NVal napiNotifyMessage = NVal::CreateObject(param->callback_->env_); napiNotifyMessage.AddProp("deviceType", NVal::CreateInt32(param->callback_->env_, param->message_.deviceType).val_); @@ -77,6 +85,8 @@ int NapiNotifyCallback::OnNotify(const NotifyMessage& message) if (ret != napi_ok) { HILOG_ERROR("Notify failed, status:%{public}d.", ret); } + // 关闭handle scope释放napi_value + napi_close_handle_scope(param->callback_->env_, scope); delete param; delete work; }); -- Gitee