From a13a244526e4110739872904c860edd645accdd1 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Fri, 27 Mar 2015 09:31:05 -0700 Subject: [PATCH] [Issue #2617] Fixing leak for addObserver:...:withBlock: --- UIKit+AFNetworking/UIAlertView+AFNetworking.m | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/UIKit+AFNetworking/UIAlertView+AFNetworking.m b/UIKit+AFNetworking/UIAlertView+AFNetworking.m index 44ce6f8e75..6890563795 100644 --- a/UIKit+AFNetworking/UIAlertView+AFNetworking.m +++ b/UIKit+AFNetworking/UIAlertView+AFNetworking.m @@ -62,20 +62,26 @@ + (void)showAlertViewForTaskWithErrorOnCompletion:(NSURLSessionTask *)task cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION { - va_list otherTitleList; - va_start(otherTitleList, otherButtonTitles); - UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil]; - for(NSString *otherTitle = otherButtonTitles; otherTitle != nil; otherTitle = va_arg(otherTitleList, NSString *)){ - [alertView addButtonWithTitle:otherTitle]; + NSMutableArray *mutableOtherTitles = [NSMutableArray array]; + va_list otherButtonTitleList; + va_start(otherButtonTitleList, otherButtonTitles); + { + for (NSString *otherButtonTitle = otherButtonTitles; otherButtonTitle != nil; otherButtonTitle = va_arg(otherButtonTitleList, NSString *)) { + [mutableOtherTitles addObject:otherButtonTitle]; + } } - va_end(otherTitleList); - __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingTaskDidCompleteNotification object:task queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { + va_end(otherButtonTitleList); + __block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingTaskDidCompleteNotification object:task queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { NSError *error = notification.userInfo[AFNetworkingTaskDidCompleteErrorKey]; if (error) { NSString *title, *message; AFGetAlertViewTitleAndMessageFromError(error, &title, &message); + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil]; + for (NSString *otherButtonTitle in mutableOtherTitles) { + [alertView addButtonWithTitle:otherButtonTitle]; + } [alertView setTitle:title]; [alertView setMessage:message]; [alertView show]; @@ -99,14 +105,17 @@ + (void)showAlertViewForRequestOperationWithErrorOnCompletion:(AFURLConnectionOp cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION { - va_list otherTitleList; - va_start(otherTitleList, otherButtonTitles); - UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil]; - for(NSString *otherTitle = otherButtonTitles; otherTitle != nil; otherTitle = va_arg(otherTitleList, NSString *)){ - [alertView addButtonWithTitle:otherTitle]; + NSMutableArray *mutableOtherTitles = [NSMutableArray array]; + va_list otherButtonTitleList; + va_start(otherButtonTitleList, otherButtonTitles); + { + for (NSString *otherButtonTitle = otherButtonTitles; otherButtonTitle != nil; otherButtonTitle = va_arg(otherButtonTitleList, NSString *)) { + [mutableOtherTitles addObject:otherButtonTitle]; + } } - va_end(otherTitleList); - __block id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingOperationDidFinishNotification object:operation queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { + va_end(otherButtonTitleList); + + __block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:AFNetworkingOperationDidFinishNotification object:operation queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) { if (notification.object && [notification.object isKindOfClass:[AFURLConnectionOperation class]]) { NSError *error = [(AFURLConnectionOperation *)notification.object error]; @@ -114,6 +123,10 @@ + (void)showAlertViewForRequestOperationWithErrorOnCompletion:(AFURLConnectionOp NSString *title, *message; AFGetAlertViewTitleAndMessageFromError(error, &title, &message); + UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil, nil]; + for (NSString *otherButtonTitle in mutableOtherTitles) { + [alertView addButtonWithTitle:otherButtonTitle]; + } [alertView setTitle:title]; [alertView setMessage:message]; [alertView show];