修复未调用napi_delete_reference导致的内存泄露问题

Signed-off-by: taojuncun <taojuncun@huawei.com>
This commit is contained in:
taojuncun 2024-11-11 16:25:48 +08:00
parent 52b51c6692
commit 74c9a78cb0
3 changed files with 17 additions and 0 deletions

View File

@ -1055,6 +1055,7 @@ void EnableAbilityListsObserverImpl::UnsubscribeObserver(napi_env env, napi_valu
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = enableAbilityListsObservers_.begin(); iter != enableAbilityListsObservers_.end();) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) {
napi_delete_reference((*iter)->env_, (*iter)->callback_);
enableAbilityListsObservers_.erase(iter);
return;
} else {
@ -1067,6 +1068,10 @@ void EnableAbilityListsObserverImpl::UnsubscribeObservers()
{
HILOG_DEBUG();
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = enableAbilityListsObservers_.begin(); iter != enableAbilityListsObservers_.end();) {
napi_delete_reference((*iter)->env_, (*iter)->callback_);
enableAbilityListsObservers_.erase(iter);
}
enableAbilityListsObservers_.clear();
}
@ -1076,6 +1081,7 @@ void EnableAbilityListsObserverImpl::UnsubscribeInstallObserver(napi_env env, na
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end(); iter++) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->callback_)) {
napi_delete_reference((*iter)->env_, (*iter)->callback_);
installAbilityListsObservers_.erase(iter);
return;
}
@ -1086,5 +1092,9 @@ void EnableAbilityListsObserverImpl::UnsubscribeInstallObservers()
{
HILOG_DEBUG();
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = installAbilityListsObservers_.begin(); iter != installAbilityListsObservers_.end();) {
napi_delete_reference((*iter)->env_, (*iter)->callback_);
installAbilityListsObservers_.erase(iter);
}
installAbilityListsObservers_.clear();
}

View File

@ -662,6 +662,7 @@ void NAccessibilityConfigObserverImpl::UnsubscribeObserver(napi_env env,
for (auto iter = observers_.begin(); iter != observers_.end();) {
if ((*iter)->configId_ == id) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
napi_delete_reference((*iter)->env_, (*iter)->handlerRef_);
observers_.erase(iter);
return;
} else {
@ -679,6 +680,7 @@ void NAccessibilityConfigObserverImpl::UnsubscribeObservers(OHOS::AccessibilityC
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = observers_.begin(); iter != observers_.end();) {
if ((*iter)->configId_ == id) {
napi_delete_reference((*iter)->env_, (*iter)->handlerRef_);
iter = observers_.erase(iter);
} else {
iter++;

View File

@ -1413,6 +1413,7 @@ void StateListenerImpl::UnsubscribeObserver(napi_env env, napi_value observer)
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = observers_.begin(); iter != observers_.end();) {
if (CheckObserverEqual(env, observer, (*iter)->env_, (*iter)->handlerRef_)) {
napi_delete_reference((*iter)->env_, (*iter)->handlerRef_);
observers_.erase(iter);
return;
} else {
@ -1425,5 +1426,9 @@ void StateListenerImpl::UnsubscribeObservers()
{
HILOG_INFO();
std::lock_guard<ffrt::mutex> lock(mutex_);
for (auto iter = observers_.begin(); iter != observers_.end();) {
napi_delete_reference((*iter)->env_, (*iter)->handlerRef_);
observers_.erase(iter);
}
observers_.clear();
}