提交 746bb76f 编写于 作者: S shufewhx

fix memleak problem in uv_work

Signed-off-by: Nshufewhx <wanghaoxu1@huawei.com>
上级 e5e6947f
......@@ -60,6 +60,8 @@ private:
int result;
};
int OnJsRemoteRequest(CallbackParam *jsParam);
static void RemoveParcelWrap(CallbackParam *param, bool isMessageSequence, napi_value jsData);
};
struct SendRequestParam {
......
......@@ -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<NAPI_MessageSequence *>(napiDataSequence);
} else {
ZLOGW(LOG_LABEL, "delete MessageParcel jsData");
delete reinterpret_cast<NAPI_MessageParcel *>(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<CallbackParam *>(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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<SendRequestParam *>(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<SendRequestParam *>(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;
};
......
......@@ -56,6 +56,8 @@ void NAPIDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &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<OnRemoteDiedParam *>(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<IRemoteObject> &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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册