Skip to content

Commit eebe819

Browse files
jtung-applegmarcosb
authored andcommitted
[Darwin] MTRDevice work blocks should only weakly reference self (project-chip#37757)
* [Darwin] MTRDevice work blocks should only weakly reference self * Have invokeCommands call completion even when object is gone * Revert "Have invokeCommands call completion even when object is gone" This reverts commit ad01794.
1 parent bc33b25 commit eebe819

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+10-1
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,8 @@ - (void)_scheduleNextUpdate:(UInt64)nextUpdateInSeconds
708708
{
709709
mtr_weakify(self);
710710
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (nextUpdateInSeconds * NSEC_PER_SEC)), self.queue, ^{
711-
MTR_LOG_DEBUG("%@ Timer expired, start Device Time Update", self);
712711
mtr_strongify(self);
712+
MTR_LOG_DEBUG("%@ Timer expired, start Device Time Update", self);
713713
if (self) {
714714
[self _performScheduledTimeUpdate];
715715
} else {
@@ -3659,8 +3659,12 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
36593659
for (NSArray<MTRCommandWithRequiredResponse *> * commandGroup in [commands reverseObjectEnumerator]) {
36603660
// We want to invoke all the commands in the group in order, propagating along the list of
36613661
// current responses. Build up that linked list of command invokes via chaining the completions.
3662+
mtr_weakify(self);
36623663
for (MTRCommandWithRequiredResponse * command in [commandGroup reverseObjectEnumerator]) {
36633664
auto commandInvokeBlock = ^(BOOL allSucceededSoFar, NSArray<MTRDeviceResponseValueDictionary> * previousResponses) {
3665+
mtr_strongify(self);
3666+
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands commandInvokeBlock called back with nil MTRDevice"));
3667+
36643668
[self invokeCommandWithEndpointID:command.path.endpoint
36653669
clusterID:command.path.cluster
36663670
commandID:command.path.command
@@ -3669,6 +3673,8 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
36693673
expectedValueInterval:nil
36703674
queue:self.queue
36713675
completion:^(NSArray<NSDictionary<NSString *, id> *> * responses, NSError * error) {
3676+
mtr_strongify(self);
3677+
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands invokeCommandWithEndpointID completion called back with nil MTRDevice"));
36723678
if (error != nil) {
36733679
nextCompletion(NO, [previousResponses arrayByAddingObject:@ {
36743680
MTRCommandPathKey : command.path,
@@ -3701,6 +3707,9 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
37013707
}
37023708

37033709
auto commandGroupInvokeBlock = ^(BOOL allSucceededSoFar, NSArray<MTRDeviceResponseValueDictionary> * previousResponses) {
3710+
mtr_strongify(self);
3711+
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands commandGroupInvokeBlock called back with nil MTRDevice"));
3712+
37043713
if (allSucceededSoFar == NO) {
37053714
// Don't start a new command group if something failed in the
37063715
// previous one. Note that we might be running on self.queue here, so make sure we

0 commit comments

Comments
 (0)