Jakub
Jakub

Reputation: 13860

Untraceable AFNetworking memory leak

TL;DR : Clone and check leak yourself https://github.com/JakubMazur/SO41343532/

I have a one class that handle all my networking. It's called ResponseOrganizer and in there I have a class method:

+ (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);
    }];
}

Where Connection is a single my internal connection object:

Here is the implementation:

#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

And I have a category calling right method inside AFNetworking. To simplify it look like this:

-(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;
        }
}

And when I want to make request for example in my ViewController I make it like this:

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

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

}];

And when I run it in instrument I'm always getting:

enter image description here

And it here doesn't matter it will land on success/failure block, it always cause a leak. I extract everything from this and put it on github as simple as possible. Github link: https://github.com/JakubMazur/SO41343532/

Upvotes: 2

Views: 2400

Answers (1)

degapps
degapps

Reputation: 778

The leak appears here:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

It seems that the reason is the same (or similar) as discussed here - NSURLSession holds a retained reference to the delegate.

Change you code in Connection.m like this to avoid the leak:

-(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;
}

If you need to handle multiple sessions, you may use another approach: call -[AFHTTPSessionManager invalidateSessionCancelingTasks:] when you're done with the session, e.g.:

-(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];
    }];
}

(Note: pass YES if you want to cancel pending tasks, NO otherwise).

Upvotes: 9

Related Questions