3

It seems there's a memory leak in ios 8 when using the NSURLConnection sendAsynchronousRequest:queue:completionHandler: method.

(You can for example put it in the application:didFinishLaunchingWithOptions: method of AppDelegate

The following code reproduces the issue.

for(int i = 1;i<5000;i++){
    NSURL* url = [NSURL URLWithString:@"https://www.google.fr/images/srpr/logo11w.png"];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%d",i);
    }];
}

When executed, the memory keeps growing, and when each request is finished, the RAM is about 200MB.

Note that in ios 7, this does not occure.

Does any body know a fix for this ?

Drico
  • 1,284
  • 15
  • 33
  • Did you tried to create other objects after this loop. It's possible that the memory will go down when you move to a different screen or you create new objects. The fact that the memory goes up it's not necessarily a mem leak. Also you should try to use `NSURLSession`. – danypata Jan 29 '15 at 17:19
  • Did you run it with Leaks to determine which objects are still in memory? The documentation doesn't specify what the lifetime of objects that you send into "sendAsynchronousRequest:queue:completionHandler: is unfortunately. Perhaps something triggers a cleanup operation on the NSURLConnection class object which will return memory to normal. Also it might be caching the request. – KirkSpaziani Jan 29 '15 at 17:34
  • Creating a new NSOperationQueue every time seems a bit odd. What manages the lifetime of those queues? – Carl Lindberg Feb 03 '15 at 04:07
  • Can you file a bug on radar (bugreport.apple.com)? I've filed a bug report (19871483), it looks like iOS 8 is leaking with NSURLConnection. We also see this problem in our apps. – user1687195 Feb 18 '15 at 06:10
  • @user1687195 Thank you for the advice, I filed a bug report (19873335) – Drico Feb 18 '15 at 12:13

0 Answers0