Skip to content

Commit 77a6c98

Browse files
committed
Fix disposing of CancellationTokenSource with registrations.
1 parent 35cb67f commit 77a6c98

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

Bolts/Common/BFCancellationToken.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ - (void)dispose {
124124
if (self.disposed) {
125125
return;
126126
}
127+
[self.registrations makeObjectsPerformSelector:@selector(dispose)];
128+
self.registrations = nil;
127129
self.disposed = YES;
128-
for (BFCancellationTokenRegistration *registration in self.registrations) {
129-
[registration dispose];
130-
}
131-
[self.registrations removeAllObjects];
132130
}
133131
}
134132

BoltsTests/CancellationTests.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,12 @@ - (void)testDispose {
7979
XCTAssertThrowsSpecificNamed(cts.token.cancellationRequested, NSException, NSInternalInconsistencyException);
8080
}
8181

82+
- (void)testDisposeRegistrationBeforeCancellationToken {
83+
BFCancellationTokenSource *cts = [BFCancellationTokenSource cancellationTokenSource];
84+
BFCancellationTokenRegistration *registration = [cts.token registerCancellationObserverWithBlock:^{ }];
85+
86+
[cts dispose];
87+
XCTAssertNoThrow([registration dispose]);
88+
}
89+
8290
@end

0 commit comments

Comments
 (0)