From 746bb76fc8728ec6020682a3e145338a775a5862 Mon Sep 17 00:00:00 2001 From: shufewhx Date: Mon, 29 May 2023 12:36:55 +0000 Subject: [PATCH] fix memleak problem in uv_work Signed-off-by: shufewhx --- .../include/napi_remote_object_internal.h | 2 + .../napi_common/source/napi_remote_object.cpp | 44 +++++++++++++++++++ .../source/napi_remote_proxy_holder.cpp | 3 ++ 3 files changed, 49 insertions(+) diff --git a/ipc/native/src/napi_common/include/napi_remote_object_internal.h b/ipc/native/src/napi_common/include/napi_remote_object_internal.h index b9a1f74..ec1b275 100644 --- a/ipc/native/src/napi_common/include/napi_remote_object_internal.h +++ b/ipc/native/src/napi_common/include/napi_remote_object_internal.h @@ -60,6 +60,8 @@ private: int result; }; int OnJsRemoteRequest(CallbackParam *jsParam); + + static void RemoveParcelWrap(CallbackParam *param, bool isMessageSequence, napi_value jsData); }; struct SendRequestParam { diff --git a/ipc/native/src/napi_common/source/napi_remote_object.cpp b/ipc/native/src/napi_common/source/napi_remote_object.cpp index e9aebcc..0949640 100644 --- a/ipc/native/src/napi_common/source/napi_remote_object.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_object.cpp @@ -321,6 +321,23 @@ void NAPI_RemoteObject_resetOldCallingInfo(napi_env env, NAPI_CallingInfo &oldCa napi_set_named_property(env, global, "activeStatus_", oldCallingInfo.activeStatus); } +void NAPIRemoteObject::RemoveParcelWrap(CallbackParam *param, bool isMessageSequence, napi_value jsData) +{ + NAPI_MessageSequence *napiDataSequence = nullptr; + napi_remove_wrap(param->env, jsData, (void **)&napiDataSequence); + if (napiDataSequence == nullptr) { + ZLOGE(LOG_LABEL, "RemoveParcelWrap fail, wrap is empty!"); + return; + } + if (isMessageSequence) { + ZLOGW(LOG_LABEL, "delete MessageSequence jsData"); + delete reinterpret_cast(napiDataSequence); + } else { + ZLOGW(LOG_LABEL, "delete MessageParcel jsData"); + delete reinterpret_cast(napiDataSequence); + } +} + int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) { uv_loop_s *loop = nullptr; @@ -337,6 +354,8 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { ZLOGI(LOG_LABEL, "enter thread pool"); CallbackParam *param = reinterpret_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(param->env, &scope); napi_value onRemoteRequest = nullptr; napi_value thisVar = nullptr; napi_get_reference_value(param->env, param->thisVarRef, &thisVar); @@ -346,6 +365,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_get_named_property(param->env, thisVar, "onRemoteMessageRequest", &onRemoteRequest); @@ -355,6 +375,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_valuetype type = napi_undefined; @@ -368,6 +389,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } isOnRemoteMessageRequest = false; @@ -383,6 +405,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_value jsOptionConstructor = nullptr; @@ -393,6 +416,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_value jsOption; @@ -409,6 +433,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_value jsParcelConstructor = nullptr; @@ -423,6 +448,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_value jsData; @@ -436,6 +462,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } size_t argc3 = 1; @@ -447,6 +474,7 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + napi_close_handle_scope(param->env, scope); return; } napi_value jsReply; @@ -460,6 +488,8 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsData); + napi_close_handle_scope(param->env, scope); return; } size_t argc4 = 1; @@ -471,6 +501,8 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsData); + napi_close_handle_scope(param->env, scope); return; } NAPI_CallingInfo oldCallingInfo; @@ -556,12 +588,18 @@ int NAPIRemoteObject::OnJsRemoteRequest(CallbackParam *jsParam) param->result = ERR_UNKNOWN_TRANSACTION; break; } + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsData); + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsReply); + napi_close_handle_scope(param->env, scope); return; } while (0); std::unique_lock lock(param->lockInfo->mutex); param->lockInfo->ready = true; param->lockInfo->condition.notify_all(); + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsData); + RemoveParcelWrap(param, isOnRemoteMessageRequest, jsReply); + napi_close_handle_scope(param->env, scope); }); std::unique_lock lock(jsParam->lockInfo->mutex); jsParam->lockInfo->condition.wait(lock, [&jsParam] { return jsParam->lockInfo->ready; }); @@ -802,6 +840,8 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) afterWorkCb = [](uv_work_t *work, int status) { ZLOGI(LOG_LABEL, "callback started"); SendRequestParam *param = reinterpret_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(param->env, &scope); napi_value result = MakeSendRequestResult(param); napi_value callback = nullptr; napi_get_reference_value(param->env, param->callback, &callback); @@ -811,6 +851,7 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) napi_delete_reference(param->env, param->jsDataRef); napi_delete_reference(param->env, param->jsReplyRef); napi_delete_reference(param->env, param->callback); + napi_close_handle_scope(param->env, scope); delete param; delete work; }; @@ -818,6 +859,8 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) afterWorkCb = [](uv_work_t *work, int status) { ZLOGI(LOG_LABEL, "promise fullfilled"); SendRequestParam *param = reinterpret_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(param->env, &scope); napi_value result = MakeSendRequestResult(param); if (param->errCode == 0) { napi_resolve_deferred(param->env, param->deferred, result); @@ -827,6 +870,7 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) napi_delete_reference(param->env, param->jsCodeRef); napi_delete_reference(param->env, param->jsDataRef); napi_delete_reference(param->env, param->jsReplyRef); + napi_close_handle_scope(param->env, scope); delete param; delete work; }; diff --git a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp index fc1d53f..144cb03 100644 --- a/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp +++ b/ipc/native/src/napi_common/source/napi_remote_proxy_holder.cpp @@ -56,6 +56,8 @@ void NAPIDeathRecipient::OnRemoteDied(const wptr &object) uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { ZLOGI(LOG_LABEL, "start to call onRmeoteDied"); OnRemoteDiedParam *param = reinterpret_cast(work->data); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(param->env, &scope); napi_value jsDeathRecipient = nullptr; napi_get_reference_value(param->env, param->deathRecipientRef, &jsDeathRecipient); NAPI_ASSERT_RETURN_VOID(param->env, jsDeathRecipient != nullptr, "failed to get js death recipient"); @@ -69,6 +71,7 @@ void NAPIDeathRecipient::OnRemoteDied(const wptr &object) } napi_status napiStatus = napi_delete_reference(param->env, param->deathRecipientRef); + napi_close_handle_scope(param->env, scope); NAPI_ASSERT_RETURN_VOID(param->env, napiStatus == napi_ok, "failed to delete ref to js death recipient"); delete param; delete work; -- GitLab