From b0864112e16c6ce3a7cbc8c7d9c94a35bbcdccf4 Mon Sep 17 00:00:00 2001 From: YOUR_NAME Date: Thu, 9 Mar 2023 19:51:45 +0800 Subject: [PATCH] fix: remove death recipient when core service client destory Signed-off-by: liujiandong --- frameworks/native/src/core_service_client.cpp | 30 ++++++++++++++----- .../innerkits/include/core_service_client.h | 1 + 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/frameworks/native/src/core_service_client.cpp b/frameworks/native/src/core_service_client.cpp index a2beb2a6b..34b4cddea 100755 --- a/frameworks/native/src/core_service_client.cpp +++ b/frameworks/native/src/core_service_client.cpp @@ -28,7 +28,10 @@ namespace Telephony { constexpr int32_t INVALID_VALUE = -1; CoreServiceClient::CoreServiceClient() = default; -CoreServiceClient::~CoreServiceClient() = default; +CoreServiceClient::~CoreServiceClient() +{ + RemoveDeathRecipient(nullptr, false); +} sptr CoreServiceClient::GetProxy() { @@ -65,21 +68,32 @@ sptr CoreServiceClient::GetProxy() void CoreServiceClient::OnRemoteDied(const wptr &remote) { - if (remote == nullptr) { - TELEPHONY_LOGE("OnRemoteDied failed, remote is nullptr"); + RemoveDeathRecipient(remote, true); +} + +void CoreServiceClient::RemoveDeathRecipient(const wptr &remote, bool isRemoteDied) +{ + if (isRemoteDied && remote == nullptr) { + TELEPHONY_LOGE("Remote died, remote is nullptr"); return; } std::lock_guard lock(mutexProxy_); if (proxy_ == nullptr) { - TELEPHONY_LOGE("OnRemoteDied proxy_ is nullptr"); + TELEPHONY_LOGE("proxy_ is nullptr"); return; } auto serviceRemote = proxy_->AsObject(); - if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) { - serviceRemote->RemoveDeathRecipient(deathRecipient_); - proxy_ = nullptr; - TELEPHONY_LOGE("on remote died"); + if (serviceRemote == nullptr) { + TELEPHONY_LOGE("serviceRemote is nullptr"); + return; } + if (isRemoteDied && serviceRemote != remote.promote()) { + TELEPHONY_LOGE("Remote died serviceRemote is not same"); + return; + } + serviceRemote->RemoveDeathRecipient(deathRecipient_); + proxy_ = nullptr; + TELEPHONY_LOGI("RemoveDeathRecipient success"); } int32_t CoreServiceClient::GetPsRadioTech(int32_t slotId, int32_t &psRadioTech) diff --git a/interfaces/innerkits/include/core_service_client.h b/interfaces/innerkits/include/core_service_client.h index 8d12542f8..7b9ac89dd 100755 --- a/interfaces/innerkits/include/core_service_client.h +++ b/interfaces/innerkits/include/core_service_client.h @@ -121,6 +121,7 @@ public: int32_t UnregisterImsRegInfoCallback(int32_t slotId, ImsServiceType imsSrvType); private: + void RemoveDeathRecipient(const wptr &remote, bool isRemoteDied); class CoreServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: explicit CoreServiceDeathRecipient(CoreServiceClient &client) : client_(client) {}