使用块,ARC泄漏sendSynchronousRequest

时间:2013-02-11 12:26:21

标签: ios memory-management automatic-ref-counting memory-leaks block

仪器显示我的代码泄漏: 泄漏:CFHTTPCookieStorage - > NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //100.0%

我使用块执行异步下载:

   NSURL *url = [NSURL URLWithString:user.user_photo];
   NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
   [NSURLConnection asyncRequestSimple:request success:^(NSData *data) {
        user.user_photo_data=data;
        NSLog(@"Image Downloaded");
        dispatch_async(dispatch_get_main_queue(), ^{
           [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
        });     

    } failure:^(NSError *error) {
        NSLog(@"Failed downloading");
    }];

我使用类别的异步实现:

NSURLConnection的+ MyExtensions.h

+ (void)asyncRequestSimple:(NSURLRequest *)request success:(void(^)(NSData* data))successBlock_ failure:(void(^)(NSError *error))failureBlock_;

NSURLConnection的+ MyExtensions.m

+ (void)asyncRequestSimple:(NSURLRequest *)request success:(void(^)(NSData* data))successBlock_ failure:(void(^)(NSError *error))failureBlock_
{

     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURLResponse *response = nil;
        NSError *error = nil;
        NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];//100% leak

        dispatch_sync(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        });

        if (error) {
            failureBlock_(error);
        } else {
            successBlock_(resultData);
        }
    });
}

为什么会泄漏?

enter image description here

enter image description here

0 个答案:

没有答案