Skip to content

Commit af3727b

Browse files
authored
[Matter.framework] Prevent a leak when using the _subscriptionPoolWor… (project-chip#35978)
1 parent a854245 commit af3727b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/darwin/Framework/CHIP/MTRDevice_Concrete.mm

+9
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,9 @@ - (void)_ensureSubscriptionForExistingDelegates:(NSString *)reason
757757
}
758758
if ([self _deviceUsesThread]) {
759759
MTR_LOG(" => %@ - device is a thread device, scheduling in pool", self);
760+
mtr_weakify(self);
760761
[self _scheduleSubscriptionPoolWork:^{
762+
mtr_strongify(self);
761763
[self->_deviceController asyncDispatchToMatterQueue:^{
762764
std::lock_guard lock(self->_lock);
763765
[self _setupSubscriptionWithReason:[NSString stringWithFormat:@"%@ and scheduled subscription is happening", reason]];
@@ -1165,10 +1167,13 @@ - (void)_scheduleSubscriptionPoolWork:(dispatch_block_t)workBlock inNanoseconds:
11651167
return;
11661168
}
11671169

1170+
mtr_weakify(self);
11681171
dispatch_block_t workBlockToQueue = ^{
1172+
mtr_strongify(self);
11691173
// In the case where a resubscription triggering event happened and already established, running the work block should result in a no-op
11701174
MTRAsyncWorkItem * workItem = [[MTRAsyncWorkItem alloc] initWithQueue:self.queue];
11711175
[workItem setReadyHandler:^(id _Nonnull context, NSInteger retryCount, MTRAsyncWorkCompletionBlock _Nonnull completion) {
1176+
mtr_strongify(self);
11721177
MTR_LOG("%@ - work item is ready to attempt pooled subscription", self);
11731178
os_unfair_lock_lock(&self->_lock);
11741179
#ifdef DEBUG
@@ -1246,7 +1251,9 @@ - (void)_handleResubscriptionNeededWithDelayOnDeviceQueue:(NSNumber *)resubscrip
12461251

12471252
// Use the existing _triggerResubscribeWithReason mechanism, which does the right checks when
12481253
// this block is run -- if other triggering events had happened, this would become a no-op.
1254+
mtr_weakify(self);
12491255
auto resubscriptionBlock = ^{
1256+
mtr_strongify(self);
12501257
[self->_deviceController asyncDispatchToMatterQueue:^{
12511258
[self _triggerResubscribeWithReason:@"ResubscriptionNeeded timer fired" nodeLikelyReachable:NO];
12521259
} errorHandler:^(NSError * _Nonnull error) {
@@ -1357,7 +1364,9 @@ - (void)_doHandleSubscriptionReset:(NSNumber * _Nullable)retryDelay
13571364

13581365
// Call _reattemptSubscriptionNowIfNeededWithReason when timer fires - if subscription is
13591366
// in a better state at that time this will be a no-op.
1367+
mtr_weakify(self);
13601368
auto resubscriptionBlock = ^{
1369+
mtr_strongify(self);
13611370
[self->_deviceController asyncDispatchToMatterQueue:^{
13621371
std::lock_guard lock(self->_lock);
13631372
[self _reattemptSubscriptionNowIfNeededWithReason:@"got subscription reset"];

0 commit comments

Comments
 (0)