From 7a4f84f311ebd29857cef880c753a732dd723877 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 1 Aug 2022 18:37:11 -0400 Subject: [PATCH] Don't touch member variables from async dispatch. (#21456) We were null-checking members, then calling them from an async block, by which point they might be null. Fixes https://github.com/project-chip/connectedhomeip/issues/21453 --- src/darwin/Framework/CHIP/MTRBaseDevice.mm | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/darwin/Framework/CHIP/MTRBaseDevice.mm b/src/darwin/Framework/CHIP/MTRBaseDevice.mm index c1a54ac7b6983d..05a0eb023a0c74 100644 --- a/src/darwin/Framework/CHIP/MTRBaseDevice.mm +++ b/src/darwin/Framework/CHIP/MTRBaseDevice.mm @@ -1384,16 +1384,20 @@ - (instancetype)initWithPath:(const ConcreteEventPath &)path { __block NSArray * attributeReports = mAttributeReports; mAttributeReports = nil; + __block auto attributeCallback = mAttributeReportCallback; + __block NSArray * eventReports = mEventReports; mEventReports = nil; - if (mAttributeReportCallback && attributeReports.count) { + __block auto eventCallback = mEventReportCallback; + + if (attributeCallback != nil && attributeReports.count) { dispatch_async(mQueue, ^{ - mAttributeReportCallback(attributeReports); + attributeCallback(attributeReports); }); } - if (mEventReportCallback && eventReports.count) { + if (eventCallback != nil && eventReports.count) { dispatch_async(mQueue, ^{ - mEventReportCallback(eventReports); + eventCallback(eventReports); }); } }