mirror of
https://gitee.com/openharmony/msdp_device_status
synced 2024-11-23 15:40:16 +00:00
Fix Safety Warning Of Double-Free
Signed-off-by: wuliangdong <wuliangdong1@huawei.com> Change-Id: I937e4754a6448d713b2081ca29e4bb8c8cf77a42
This commit is contained in:
parent
af2f3baaed
commit
c791e35767
@ -101,10 +101,6 @@ DeviceStatusNapi::~DeviceStatusNapi()
|
||||
if (devicestatusValueRef_ != nullptr) {
|
||||
napi_delete_reference(env_, devicestatusValueRef_);
|
||||
}
|
||||
if (g_obj != nullptr) {
|
||||
delete g_obj;
|
||||
g_obj = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceStatusNapi::OnDeviceStatusChangedDone(int32_t type, int32_t value, bool isOnce)
|
||||
@ -339,14 +335,31 @@ napi_value DeviceStatusNapi::SubscribeDeviceStatusCallback(napi_env env, napi_ca
|
||||
CHKPP(g_obj);
|
||||
FI_HILOGD("Didn't find object, so created it");
|
||||
}
|
||||
napi_wrap(env, nullptr, reinterpret_cast<void *>(g_obj),
|
||||
napi_value thisArg = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
|
||||
if (status != napi_ok) {
|
||||
FI_HILOGE("Failed to get_cb_info item");
|
||||
delete g_obj;
|
||||
g_obj = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
status = napi_wrap(env, thisArg, reinterpret_cast<void *>(g_obj),
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
(void)env;
|
||||
(void)hint;
|
||||
CHKPV(data);
|
||||
DeviceStatusNapi *devicestatus = static_cast<DeviceStatusNapi *>(data);
|
||||
delete devicestatus;
|
||||
g_obj = nullptr;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
FI_HILOGE("napi_wrap failed");
|
||||
delete g_obj;
|
||||
g_obj = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
if (!g_obj->On(type, handler, false)) {
|
||||
FI_HILOGE("type:%{public}d already exists", type);
|
||||
return nullptr;
|
||||
@ -441,17 +454,34 @@ napi_value DeviceStatusNapi::GetDeviceStatus(napi_env env, napi_callback_info in
|
||||
FI_HILOGE("Once:Failed to GetDeviceStatus");
|
||||
return nullptr;
|
||||
}
|
||||
napi_value thisArg = nullptr;
|
||||
void *data = nullptr;
|
||||
napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
|
||||
if (status != napi_ok) {
|
||||
FI_HILOGE("Failed to get_cb_info item");
|
||||
delete g_obj;
|
||||
g_obj = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
if (g_obj == nullptr) {
|
||||
g_obj = new (std::nothrow) DeviceStatusNapi(env);
|
||||
CHKPP(g_obj);
|
||||
napi_wrap(env, nullptr, reinterpret_cast<void *>(g_obj),
|
||||
status = napi_wrap(env, thisArg, reinterpret_cast<void *>(g_obj),
|
||||
[](napi_env env, void *data, void *hint) {
|
||||
(void)env;
|
||||
(void)hint;
|
||||
CHKPV(data);
|
||||
DeviceStatusNapi *devicestatus = static_cast<DeviceStatusNapi *>(data);
|
||||
delete devicestatus;
|
||||
g_obj = nullptr;
|
||||
},
|
||||
nullptr, nullptr);
|
||||
if (status != napi_ok) {
|
||||
FI_HILOGE("napi_wrap failed");
|
||||
delete g_obj;
|
||||
g_obj = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
if (!g_obj->On(type, handler, true)) {
|
||||
FI_HILOGE("type:%{public}d already exists", type);
|
||||
|
Loading…
Reference in New Issue
Block a user