Authored by Joshua Feldman

#1807 - #1821 - Fixing incorrectly retained pointer to self which appears to cre…

…ate a dangled pointer
@@ -169,8 +169,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary; @@ -169,8 +169,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
169 for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) { 169 for (SDWebImageDownloaderProgressBlock progressBlock in [self callbacksForKey:kProgressCallbackKey]) {
170 progressBlock(0, NSURLResponseUnknownLength, self.request.URL); 170 progressBlock(0, NSURLResponseUnknownLength, self.request.URL);
171 } 171 }
  172 + __weak typeof(self) weakSelf = self;
172 dispatch_async(dispatch_get_main_queue(), ^{ 173 dispatch_async(dispatch_get_main_queue(), ^{
173 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self]; 174 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:weakSelf];
174 }); 175 });
175 } else { 176 } else {
176 [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Connection can't be initialized"}]]; 177 [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Connection can't be initialized"}]];
@@ -201,8 +202,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary; @@ -201,8 +202,9 @@ typedef NSMutableDictionary<NSString *, id> SDCallbacksDictionary;
201 202
202 if (self.dataTask) { 203 if (self.dataTask) {
203 [self.dataTask cancel]; 204 [self.dataTask cancel];
  205 + __weak typeof(self) weakSelf = self;
204 dispatch_async(dispatch_get_main_queue(), ^{ 206 dispatch_async(dispatch_get_main_queue(), ^{
205 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self]; 207 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf];
206 }); 208 });
207 209
208 // As we cancelled the connection, its callback won't be called and thus won't 210 // As we cancelled the connection, its callback won't be called and thus won't
@@ -266,8 +268,9 @@ didReceiveResponse:(NSURLResponse *)response @@ -266,8 +268,9 @@ didReceiveResponse:(NSURLResponse *)response
266 268
267 self.imageData = [[NSMutableData alloc] initWithCapacity:expected]; 269 self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
268 self.response = response; 270 self.response = response;
  271 + __weak typeof(self) weakSelf = self;
269 dispatch_async(dispatch_get_main_queue(), ^{ 272 dispatch_async(dispatch_get_main_queue(), ^{
270 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:self]; 273 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadReceiveResponseNotification object:weakSelf];
271 }); 274 });
272 } 275 }
273 else { 276 else {
@@ -280,8 +283,9 @@ didReceiveResponse:(NSURLResponse *)response @@ -280,8 +283,9 @@ didReceiveResponse:(NSURLResponse *)response
280 } else { 283 } else {
281 [self.dataTask cancel]; 284 [self.dataTask cancel];
282 } 285 }
  286 + __weak typeof(self) weakSelf = self;
283 dispatch_async(dispatch_get_main_queue(), ^{ 287 dispatch_async(dispatch_get_main_queue(), ^{
284 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self]; 288 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf];
285 }); 289 });
286 290
287 [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:((NSHTTPURLResponse *)response).statusCode userInfo:nil]]; 291 [self callCompletionBlocksWithError:[NSError errorWithDomain:NSURLErrorDomain code:((NSHTTPURLResponse *)response).statusCode userInfo:nil]];
@@ -402,10 +406,11 @@ didReceiveResponse:(NSURLResponse *)response @@ -402,10 +406,11 @@ didReceiveResponse:(NSURLResponse *)response
402 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error { 406 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
403 @synchronized(self) { 407 @synchronized(self) {
404 self.dataTask = nil; 408 self.dataTask = nil;
  409 + __weak typeof(self) weakSelf = self;
405 dispatch_async(dispatch_get_main_queue(), ^{ 410 dispatch_async(dispatch_get_main_queue(), ^{
406 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self]; 411 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:weakSelf];
407 if (!error) { 412 if (!error) {
408 - [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self]; 413 + [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:weakSelf];
409 } 414 }
410 }); 415 });
411 } 416 }