AFNetworking memory leak

Asked by At

I used code below to start a download

AFURLSessionManager *downloadManager1  = [[AFURLSessionManager alloc] initWithSessionConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];


NSURLSessionDownloadTask *downloadTask;
downloadTask=[downloadManager1 downloadTaskWithRequest:request progress:nil

        destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){

        NSURL *aURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

        return  [aURL URLByAppendingPathComponent:[response suggestedFilename]];

    }completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

    }
];
[downloadTask resume];

enter image description here

It reported memory leak, I checked the code above but can not find any place is wrong,

your comment welcome

3 Answers

0
Kohls On Best Solutions
invalidateSessionCancelingTasks:

This is known and documented behavior. When you're finished with a session, call invalidateSessionCancelingTasks:. This is not an issue for most apps, which keep a single session for the lifetime of the application.

https://github.com/AFNetworking/AFNetworking/issues/1528#issuecomment-26887778

0
Nicholas Smith On

You have two pointers, one to downloadManager1 and one to an NSURL object. Depending where that is being invoked in your code I suspect that there's a permanent strong reference to one or the other. Try converting the NSURL from a pointer to an actual object and it'll copy by value instead, and check to make sure the downloadManager is being released when the work is finished.

0
asaini On

I think the leak happens when you create the manager. NSURLSession holds a retained reference to the delegate. It might help if you initialize and store the manager just once. I am not sure how many times is your code for datatask is called.

Checkout https://github.com/AFNetworking/AFNetworking/issues/3787