Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BFTask retain cycle #284

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Bolts/Common/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ - (instancetype)init {
return self;
}

- (void)dealloc {
if (![self isCompleted]) {
[self trySetCancelled];
}
}

- (instancetype)initWithResult:(id)result {
self = [super init];
if (!self) return self;
Expand Down Expand Up @@ -379,8 +385,8 @@ - (void)runContinuations {
[self.condition lock];
[self.condition broadcast];
[self.condition unlock];
for (void (^callback)() in self.callbacks) {
callback();
for (void (^callback)(BFTask*) in self.callbacks) {
callback(self);
}
[self.callbacks removeAllObjects];
}
Expand All @@ -398,7 +404,7 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];

// Capture all of the state that needs to used when the continuation is complete.
dispatch_block_t executionBlock = ^{
void(^executionBlock)(BFTask*) = ^(BFTask* task){
if (cancellationToken.cancellationRequested) {
[tcs cancel];
return;
Expand All @@ -409,7 +415,7 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (BFTaskCatchesExceptions()) {
@try {
result = block(self);
result = block(task);
} @catch (NSException *exception) {
NSLog(@"[Bolts] Warning: `BFTask` caught an exception in the continuation block."
@" This behavior is discouraged and will be removed in a future release."
Expand All @@ -418,7 +424,7 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor
return;
}
} else {
result = block(self);
result = block(task);
}
#pragma clang diagnostic pop

Expand Down Expand Up @@ -457,13 +463,17 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor
@synchronized(self.lock) {
completed = self.completed;
if (!completed) {
[self.callbacks addObject:[^{
[executor execute:executionBlock];
[self.callbacks addObject:[^(BFTask* task){
[executor execute:^{
executionBlock(task);
}];
} copy]];
}
}
if (completed) {
[executor execute:executionBlock];
[executor execute:^{
executionBlock(self);
}];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove block

}

return tcs.task;
Expand Down