From 570d631897d89c1d536598dfbb600c4e43a76133 Mon Sep 17 00:00:00 2001 From: harrey Date: Thu, 21 Nov 2024 21:05:04 +0800 Subject: [PATCH] Bugfix for CoreServiceClient lifecyle. Signed-off-by: harrey --- frameworks/native/src/core_service_client.cpp | 3 +++ interfaces/innerkits/include/core_service_client.h | 13 +++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/frameworks/native/src/core_service_client.cpp b/frameworks/native/src/core_service_client.cpp index 88e9a18d7..dd86528ef 100644 --- a/frameworks/native/src/core_service_client.cpp +++ b/frameworks/native/src/core_service_client.cpp @@ -31,6 +31,9 @@ CoreServiceClient::CoreServiceClient() = default; CoreServiceClient::~CoreServiceClient() { RemoveDeathRecipient(nullptr, false); + if (deathRecipient_ != nullptr) { + reinterpret_cast(deathRecipient_.GetRefPtr())->SetValid(false); + } } sptr CoreServiceClient::GetProxy() diff --git a/interfaces/innerkits/include/core_service_client.h b/interfaces/innerkits/include/core_service_client.h index 51daba449..cf16dd46a 100644 --- a/interfaces/innerkits/include/core_service_client.h +++ b/interfaces/innerkits/include/core_service_client.h @@ -16,6 +16,7 @@ #ifndef CORE_SERVICE_CLIENT_H #define CORE_SERVICE_CLIENT_H +#include #include #include #include @@ -1212,15 +1213,23 @@ private: void RemoveDeathRecipient(const wptr &remote, bool isRemoteDied); class CoreServiceDeathRecipient : public IRemoteObject::DeathRecipient { public: - explicit CoreServiceDeathRecipient(CoreServiceClient &client) : client_(client) {} + explicit CoreServiceDeathRecipient(CoreServiceClient &client) : client_(client), isValid_(true) {} ~CoreServiceDeathRecipient() override = default; __attribute__((no_sanitize("cfi"))) void OnRemoteDied(const wptr &remote) override { - client_.OnRemoteDied(remote); + if (isValid_.load()) { + client_.OnRemoteDied(remote); + } + } + + void SetValid(bool isValid) + { + isValid_.store(isValid); } private: CoreServiceClient &client_; + std::atomic_bool isValid_; }; private: