Afnetworking: Potential Memory Leaks in 3.0.4

Created on 15 Jan 2016  ·  14Comments  ·  Source: AFNetworking/AFNetworking

When I called the GET or POST method to get some Json data,there will be so many leaks listed in Instruments->leaks tool. There is my code:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"xxx" parameters:nil progress: nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

IDE: Xcode7.2

needs more information stale

Most helpful comment

Do not use a class func.Make sure your manager is a global variable

All 14 comments

I also use this way Batch requests ,there are so many leaks when use AF3.0.4

I profiled the example project again this morning, and don't see any leaks occurring. You could save your Instruments trace and attach it here, or create a small sample project that reproduces the problem?

i'm found some memory leak,please MR kcharwood have a look,and tell me the reason,thanks.
Uploading Instruments2.trace.zip…
Uploading Instruments4.trace.zip…

+1

I have the same problem when I simply use the GET method.Here is my code:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"https://api.app.net/stream/0/posts/stream/global" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"response:%@", responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@", error);
    }];

AFNetworking version: 3.1.0 and here is the example trace file:
AFNetworking/ExampleMemoryLeak.trace.zip

+1

I used 3.1.0,and it still leaks.Here is my code:

@implementation HttpTool
……………………
+ (void)get:(NSString *)urlString params:(NSDictionary *)params success:(void (^)(id json))success    failure:(void (^)(NSError *error))failure {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer =  [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
    [manager GET:[kBaseUrl stringByAppendingString:urlString]  parameters:params progress:nil   success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {     
        if (failure) {
            failure(error);
        }
    }];
}
@end

Do not use a class func.Make sure your manager is a global variable

Here is a leaks demo. @kcharwood
AFNDemo.zip

The reason is what Apple said in NSURLSession Guide probably :

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until it exits.

emmmmm.........
I thinked a lot ,but I can't see any solution to fix this problem IN AFNetworking,

One solution is make AFHTTPURLSessionManager to a singleton, but I can only config ONE timeoutInterval and some other configuration in manager.

Another solution is when my request task finished ,I called invalidateSessionCancelingTasks: to my manager, but this will cancel other tasks in this manager.

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 🙏 Also, feel free to open a new issue if you still experience this problem 👍.

Was this page helpful?
0 / 5 - 0 ratings