Memory leak around NSURLSession

纵然是瞬间 提交于 2019-12-12 03:24:37

问题


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

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:


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



来源:https://stackoverflow.com/questions/38361174/memory-leak-around-nsurlsession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!