fix crash when release bundlestatus callback

Signed-off-by: longwei <longwei27@huawei.com>
Change-Id: Icabe2cc0f48b7f14b93f3d675ce5135cdf0cefc1
This commit is contained in:
longwei
2022-03-17 22:22:17 +08:00
parent 9683c79d2e
commit dc8d3bb536
3 changed files with 46 additions and 3 deletions
@@ -42,6 +42,7 @@ bool BundleMonitor::UnSubscribe()
APP_LOGE("UnsubscribeCommonEvent occur exception.");
return false;
}
callback_ = nullptr;
return true;
}
@@ -26,9 +26,44 @@ BundleStatusCallback::BundleStatusCallback(napi_env env, napi_ref addedCallback,
BundleStatusCallback::~BundleStatusCallback()
{
napi_delete_reference(env_, addedCallback_);
napi_delete_reference(env_, updatedCallback_);
napi_delete_reference(env_, removeCallback_);
uv_loop_s* loop = nullptr;
napi_get_uv_event_loop(env_, &loop);
uv_work_t* work = new uv_work_t;
DelRefCallbackInfo* delRefCallbackInfo = new DelRefCallbackInfo {
.env_ = env_,
.addedCallback_ = addedCallback_,
.updatedCallback_ = updatedCallback_,
.removeCallback_ = removeCallback_,
};
if (work == nullptr || delRefCallbackInfo == nullptr) {
return;
}
work->data = (void*)delRefCallbackInfo;
int ret = uv_queue_work(
loop, work, [](uv_work_t* work) {},
[](uv_work_t* work, int status) {
// JS Thread
DelRefCallbackInfo* delRefCallbackInfo = reinterpret_cast<DelRefCallbackInfo*>(work->data);
napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->addedCallback_);
napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->updatedCallback_);
napi_delete_reference(delRefCallbackInfo->env_, delRefCallbackInfo->removeCallback_);
if (delRefCallbackInfo != nullptr) {
delete delRefCallbackInfo;
delRefCallbackInfo = nullptr;
}
if (work != nullptr) {
delete work;
work = nullptr;
}
});
if (ret != 0) {
if (delRefCallbackInfo != nullptr) {
delete delRefCallbackInfo;
}
if (work != nullptr) {
delete work;
}
}
}
void BundleStatusCallback::OnBundleAdded(const std::string& bundleName, const int userId)
@@ -50,4 +50,11 @@ struct AsyncCallbackInfo {
int32_t userId_;
};
struct DelRefCallbackInfo {
napi_env env_;
napi_ref addedCallback_;
napi_ref updatedCallback_;
napi_ref removeCallback_;
};
#endif // LAUNCHER_STATUS_CALLBACK_H