Memory leak around NSURLSession

2019-09-12 22:22发布

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

1条回答
爷的心禁止访问
2楼-- · 2019-09-12 23:00

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

查看更多
登录 后发表回答