Skip to content

Commit

Permalink
[Darwin] MTRDevice work blocks should only weakly reference self
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Feb 25, 2025
1 parent ff7398a commit da22b2d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/darwin/Framework/CHIP/MTRDevice_Concrete.mm
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,8 @@ - (void)_scheduleNextUpdate:(UInt64)nextUpdateInSeconds
{
mtr_weakify(self);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (nextUpdateInSeconds * NSEC_PER_SEC)), self.queue, ^{
MTR_LOG_DEBUG("%@ Timer expired, start Device Time Update", self);
mtr_strongify(self);
MTR_LOG_DEBUG("%@ Timer expired, start Device Time Update", self);
if (self) {
[self _performScheduledTimeUpdate];
} else {
Expand Down Expand Up @@ -3659,8 +3659,12 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
for (NSArray<MTRCommandWithRequiredResponse *> * commandGroup in [commands reverseObjectEnumerator]) {
// We want to invoke all the commands in the group in order, propagating along the list of
// current responses. Build up that linked list of command invokes via chaining the completions.
mtr_weakify(self);
for (MTRCommandWithRequiredResponse * command in [commandGroup reverseObjectEnumerator]) {
auto commandInvokeBlock = ^(BOOL allSucceededSoFar, NSArray<MTRDeviceResponseValueDictionary> * previousResponses) {
mtr_strongify(self);
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands commandInvokeBlock called back with nil MTRDevice"));

[self invokeCommandWithEndpointID:command.path.endpoint
clusterID:command.path.cluster
commandID:command.path.command
Expand All @@ -3669,6 +3673,8 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
expectedValueInterval:nil
queue:self.queue
completion:^(NSArray<NSDictionary<NSString *, id> *> * responses, NSError * error) {
mtr_strongify(self);
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands invokeCommandWithEndpointID completion called back with nil MTRDevice"));
if (error != nil) {
nextCompletion(NO, [previousResponses arrayByAddingObject:@ {
MTRCommandPathKey : command.path,
Expand Down Expand Up @@ -3701,6 +3707,9 @@ - (void)invokeCommands:(NSArray<NSArray<MTRCommandWithRequiredResponse *> *> *)c
}

auto commandGroupInvokeBlock = ^(BOOL allSucceededSoFar, NSArray<MTRDeviceResponseValueDictionary> * previousResponses) {
mtr_strongify(self);
VerifyOrReturn(self, MTR_LOG_DEBUG("invokeCommands commandGroupInvokeBlock called back with nil MTRDevice"));

if (allSucceededSoFar == NO) {
// Don't start a new command group if something failed in the
// previous one. Note that we might be running on self.queue here, so make sure we
Expand Down

0 comments on commit da22b2d

Please sign in to comment.