From ac6d99fa0ec977de341d1ae810d750fba09ba5d5 Mon Sep 17 00:00:00 2001 From: shenpeixing Date: Mon, 31 Mar 2025 19:21:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=81=B6=E5=85=88=E8=AE=A2?= =?UTF-8?q?=E9=98=85=E5=9B=9E=E8=B0=83=E6=8E=A5=E5=8F=A3crash=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shenpeixing --- frameworks/native/src/sensor_agent_proxy.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 2b10d902..2d023b4d 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -104,8 +104,13 @@ int32_t SensorAgentProxy::CreateSensorDataChannel() return ERR_OK; } CHKPR(dataChannel_, INVALID_POINTER); - auto ret = dataChannel_->CreateSensorDataChannel([this] (SensorEvent *events, int32_t num, void *data) { - this->HandleSensorData(events, num, data); + auto ret = dataChannel_->CreateSensorDataChannel( + [weakSelf = std::weak_ptr(SENSOR_AGENT_IMPL)](SensorEvent *events, int32_t num, void *data) { + if (auto sharedSelf = weakSelf.lock()) { + sharedSelf->HandleSensorData(events, num, data); + } else { + SEN_HILOGD("SensorAgentProxy object has been released"); + } }, nullptr); if (ret != ERR_OK) { SEN_HILOGE("Create data channel failed, ret:%{public}d", ret); -- Gitee