!322 loop增加空指针判定

Merge pull request !322 from 刘昊苏/master
This commit is contained in:
openharmony_ci 2024-08-29 10:06:49 +00:00 committed by Gitee
commit 30dcb3829c
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 36 additions and 23 deletions

View File

@ -82,6 +82,7 @@ public:
#ifndef TDD_ENABLE
private:
#endif
int UvQueueWork(uv_loop_s *loop, uv_work_t *work);
napi_env env_;
napi_ref ref_;
std::function<void(std::vector<ScanDeviceInfo> &infos)> callbackFunction_;

View File

@ -38,6 +38,9 @@ ScanCallback::~ScanCallback()
SCAN_HILOGI("callback has been destroyed");
uv_loop_s *loop = nullptr;
napi_get_uv_event_loop(env_, &loop);
if (loop == nullptr) {
return;
}
Param *param = new (std::nothrow) Param;
if (param == nullptr) {
return;
@ -50,29 +53,7 @@ ScanCallback::~ScanCallback()
return;
}
work->data = reinterpret_cast<void*>(param);
int retVal = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int _status) {
SCAN_HILOGI("uv_queue_work ScanCallback DeleteReference");
if (work == nullptr) {
return;
}
Param *param_ = reinterpret_cast<Param*>(work->data);
if (param_ == nullptr) {
delete work;
return;
}
napi_handle_scope scope = nullptr;
napi_open_handle_scope(param_->env, &scope);
if (scope == nullptr) {
delete param_;
delete work;
return;
}
napi_ref callbackRef_ = param_->callbackRef;
NapiScanUtils::DeleteReference(param_->env, callbackRef_);
napi_close_handle_scope(param_->env, scope);
delete param_;
delete work;
});
int retVal = UvQueueWork(loop, work);
if (retVal != 0) {
SCAN_HILOGE("Failed to get uv_queue_work.");
delete param;
@ -340,4 +321,35 @@ bool ScanCallback::OnGetDevicesList(std::vector<ScanDeviceInfo> &infos)
callbackFunction_(infos);
return true;
}
int ScanCallback::UvQueueWork(uv_loop_s *loop, uv_work_t *work)
{
if (loop == nullptr || work == nullptr) {
return -1; // parameter error
}
int retVal = uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) {
SCAN_HILOGI("uv_queue_work ScanCallback DeleteReference");
if (work == nullptr) {
return;
}
Param *param = reinterpret_cast<Param*>(work->data);
if (param == nullptr) {
delete work;
return;
}
napi_handle_scope scope = nullptr;
napi_open_handle_scope(param->env, &scope);
if (scope == nullptr) {
delete param;
delete work;
return;
}
napi_ref callbackRef = param->callbackRef;
NapiScanUtils::DeleteReference(param->env, callbackRef);
napi_close_handle_scope(param->env, scope);
delete param;
delete work;
});
return retVal;
}
} // namespace OHOS::Scan