Skip to content

Commit

Permalink
Changed enumeration to be safer
Browse files Browse the repository at this point in the history
  • Loading branch information
jtung-apple committed Sep 7, 2024
1 parent 88d1bed commit afef79f
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/darwin/Framework/CHIP/MTRDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,16 @@ - (void)suspend
@synchronized (self) {
_suspended = YES;

std::lock_guard lock(*self.deviceMapLock);
NSEnumerator * devices = [self.nodeIDToDeviceMap objectEnumerator];
for (MTRDevice * device in devices) {
NSMutableArray *devicesToSuspend = [NSMutableArray array];
{
std::lock_guard lock(*self.deviceMapLock);
NSEnumerator * devices = [self.nodeIDToDeviceMap objectEnumerator];
for (MTRDevice * device in devices) {
[devicesToSuspend addObject:device];
}
}

for (MTRDevice * device in devicesToSuspend) {
[device controllerSuspended];
}

Expand All @@ -383,9 +390,16 @@ - (void)resume
@synchronized (self) {
_suspended = NO;

std::lock_guard lock(*self.deviceMapLock);
NSEnumerator * devices = [self.nodeIDToDeviceMap objectEnumerator];
for (MTRDevice * device in devices) {
NSMutableArray *devicesToResume = [NSMutableArray array];
{
std::lock_guard lock(*self.deviceMapLock);
NSEnumerator * devices = [self.nodeIDToDeviceMap objectEnumerator];
for (MTRDevice * device in devices) {
[devicesToResume addObject:device];
}
}

for (MTRDevice * device in devicesToResume) {
[device controllerResumed];
}
}
Expand Down

0 comments on commit afef79f

Please sign in to comment.