From 47767920d07ceb501c3cbbf77ff3d9ec4cec372c Mon Sep 17 00:00:00 2001 From: Jeff Tung <100387939+jtung-apple@users.noreply.github.com> Date: Thu, 9 May 2024 11:04:14 -0700 Subject: [PATCH] [Darwin] MTRDeviceConnectivityMonitor should weakify before using self in a handler block --- .../CHIP/MTRDeviceConnectivityMonitor.mm | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRDeviceConnectivityMonitor.mm b/src/darwin/Framework/CHIP/MTRDeviceConnectivityMonitor.mm index 0d65304c843d04..229d95d9206bc1 100644 --- a/src/darwin/Framework/CHIP/MTRDeviceConnectivityMonitor.mm +++ b/src/darwin/Framework/CHIP/MTRDeviceConnectivityMonitor.mm @@ -148,19 +148,26 @@ - (void)handleResolvedHostname:(const char *)hostName port:(uint16_t)port error: return; } nw_connection_set_queue(connection, sSharedResolverQueue); + mtr_weakify(self); nw_connection_set_path_changed_handler(connection, ^(nw_path_t _Nonnull path) { - nw_path_status_t status = nw_path_get_status(path); - if (status == nw_path_status_satisfied) { - MTR_LOG_INFO("%@ path is satisfied", self); - std::lock_guard lock(sConnectivityMonitorLock); - [self _callHandler]; + mtr_strongify(self); + if (self) { + nw_path_status_t status = nw_path_get_status(path); + if (status == nw_path_status_satisfied) { + MTR_LOG_INFO("%@ path is satisfied", self); + std::lock_guard lock(sConnectivityMonitorLock); + [self _callHandler]; + } } }); nw_connection_set_viability_changed_handler(connection, ^(bool viable) { - if (viable) { - std::lock_guard lock(sConnectivityMonitorLock); - MTR_LOG_INFO("%@ connectivity now viable", self); - [self _callHandler]; + mtr_strongify(self); + if (self) { + if (viable) { + std::lock_guard lock(sConnectivityMonitorLock); + MTR_LOG_INFO("%@ connectivity now viable", self); + [self _callHandler]; + } } }); nw_connection_start(connection);