无法追踪的 AFNetworking 内存泄漏

Untraceable AFNetworking memory leak

TL;DR:自己克隆并检查漏洞 https://github.com/JakubMazur/SO41343532/

我有一个 class 可以处理我所有的网络。它叫做 ResponseOrganizer 并且在那里我有一个 class 方法:

+ (void)getSth:(void (^)(NSURLSessionDataTask *operation, NSArray *locales, id plainObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {

    Connection *connection = [Connection new];
    connection.urlString = @"http://sample-file.bazadanni.com/download/txt/json/sample.json";
    connection.requestMethodType = GET;

    [connection fireWithSuccess:^(NSURLSessionDataTask *operation, NSArray *returnArray, id originalResponse) {
        success(operation, returnArray, originalResponse);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        failure(operation, error);
    }];
}

其中 Connection 是单个我的内部连接对象:

实现如下:

#import "Connection.h"

@interface Connection()
@property (weak,nonatomic) AFHTTPSessionManager *manager;
@end

@implementation Connection

#pragma mark - Connection groundwork

-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {

    self.manager = [AFHTTPSessionManager manager];
    [self.manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
        success(operation,@[responseObject],nil);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        failure(operation,error);
    }];
}

@end

而且我在AFNetworking里面有一个类别调用权方法。为了简化它看起来像这样:

-(void)urlString:(NSString*)urlString withMethod:(RequestMethodType)method parameters:(NSDictionary*)parameters success:(void (^)(NSURLSessionDataTask *operation, id responseObject))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
    switch (method) {
        case GET: {
            [self getWithURLString:urlString parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
                success(operation,responseObject);
            } failure:^(NSURLSessionDataTask *operation, NSError *error) {
                failure(operation,error);
            }];
            break;
        }
}

当我想在我的 ViewController 中提出请求时,我会这样做:

[ResponseOrginizer getSth:^(NSURLSessionDataTask *operation, NSArray *locales, id plainObject) {

} failure:^(NSURLSessionDataTask *operation, NSError *error) {

}];

当我在仪器中 运行 它时,我总是得到:

它会落在 success/failure 块上并不重要,它总是会导致泄漏。我从中提取所有内容并尽可能简单地将其放在 github 上。 Github link: https://github.com/JakubMazur/SO41343532/

泄漏出现在这里:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

似乎原因与所讨论的相同(或相似)here - NSURLSession 持有对委托的保留引用。

像这样更改 Connection.m 中的代码以避免泄漏:

-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
    AFHTTPSessionManager *manager = [Connection manager];

    [manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
        success(operation,@[responseObject],nil);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        failure(operation,error);
    }];
}

+ (AFHTTPSessionManager*) manager
{
    static dispatch_once_t onceToken;
    static AFHTTPSessionManager *manager = nil;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
    });

    return manager;
}

如果您需要处理多个会话,您可以使用另一种方法:在完成会话后调用 -[AFHTTPSessionManager invalidateSessionCancelingTasks:],例如:

-(void)fireWithSuccess:(void (^)(NSURLSessionDataTask *operation, NSArray* returnArray, id originalResponse))success failure:(void (^)(NSURLSessionDataTask *operation, NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager urlString:self.urlString withMethod:self.requestMethodType parameters:self.paramaters success:^(NSURLSessionDataTask *operation, id responseObject) {
        success(operation,@[responseObject],nil);
        [manager invalidateSessionCancelingTasks:YES];
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        failure(operation,error);
        [manager invalidateSessionCancelingTasks:YES];
    }];
}

(注意:如果要取消待处理的任务,请传递YES,否则传递NO)。