0

I am noticing a memory leak around the NSURLSession in my code.

Leak seen on Instruments

Below is the code that uses NSURLSession in my app:

-(void)createWithUserId:(NSString*)userId andAccountNumber:(NSString*)accountNumber
{

    NSURL *url = [NSURL URLWithString:[WebServiceUtility getCreateBatchURLWithBaseURL:self.baseURL]];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[WebServiceUtility getSessionConfigurationWithRequestHeader:(NSDictionary*)self.requestHeader andTimeout:self.requestTimeout]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    request.HTTPMethod = kCMDCPOSTMethod;

    NSDictionary *dictionary = self.requestBody;
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
                                                   options:kNilOptions error:&error];

    if (!error) {

        NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                                   fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {

                                                                       if([[WebServiceUtility getStatusCodeForResponse:response] isEqualToString:kCMDCStatusCode_200] && data)
                                                                       {

                                                                           NSMutableDictionary *json = [WebServiceUtility dictionaryFromData:data];
                                                                           [self.delegate createdWithResults:json];
                                                                       }
                                                                       else
                                                                       {
                                                                           NSMutableDictionary *json=[[NSMutableDictionary alloc]init];
                                                                           [json setObject:kCMDCServerError_1 forKey:kCMDCServerError];

                                                                           [json setObject:[WebServiceUtility getStatusCodeForResponse:response] forKey:kCMDCStatusCode];
                                                                           [self.delegate createdWithResults:json];
                                                                       }
                                                                   }];

        [uploadTask resume];
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • As long as you don't see any of your classes listed in here amongst the leaked memory, I wouldn't worry about it. I don't see anything terribly significant here. The only thing I might suggest is to not instantiate a new `NSURLSession` object each time you make a network request. This will minimize the memory impact of any `NSURLSession` related issues. – Rob Jul 13 '16 at 20:41
  • How are you creating your WebServiceUtility singleton (getSessionConfigurationWithRequestHeader:)? – Keller Jul 14 '16 at 03:35
  • Thanks guys for the help. I figured out the issue. I had to finishAndInvalidate the session once the block is fired. Doing that fixed the issue for me. – user2884277 Jul 14 '16 at 23:32
  • Really, you should probably be keeping the session around and reusing it, but yes, you should always invalidate it before releasing it. – dgatwood Aug 03 '16 at 04:17

1 Answers1

0

I know this is a REALLY old post, but I just came across this, and I've identified that it's an issue with an internal Apple API. I believe the leak I'm seeing is identical to yours. After chasing my tail for a whole day trying to figure out the cause, I found that the leak only shows when I have my device (or simulator) running through a proxy (such as Charles or Fiddler). Disable the proxy, and the issue goes away.

I've filed a radar with Apple for this. It's an edge case, since most end consumers won't use a proxy on their devices, but it's common that developers use proxies to debug network calls, and anyone using NSURLSession who has a proxy set WILL see this leak. Issue duplicated at Openradar: https://openradar.appspot.com/radar?id=6140533516795904

CPR
  • 753
  • 8
  • 19