diff --git a/Bolts/Common/BFTask.m b/Bolts/Common/BFTask.m index fa14fac20..6363c5ca7 100644 --- a/Bolts/Common/BFTask.m +++ b/Bolts/Common/BFTask.m @@ -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; @@ -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]; } @@ -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; @@ -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." @@ -418,7 +424,7 @@ - (BFTask *)continueWithExecutor:(BFExecutor *)executor return; } } else { - result = block(self); + result = block(task); } #pragma clang diagnostic pop @@ -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); + }]; } return tcs.task;