NSURLSession周围的内存泄漏 [英] Memory leak around NSURLSession

查看:130
本文介绍了NSURLSession周围的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到代码中NSURLSession周围的内存泄漏.

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

以下是在我的应用中使用NSURLSession的代码:

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

推荐答案

我知道这是一篇非常老的文章,但是我碰到了这一点,并且我已经确定这是内部Apple API的问题.我相信我看到的泄漏与您的泄漏相同.一整天试图找出原因后,我发现泄漏仅在我的设备(或模拟器)通过代理(例如Charles或Fiddler)运行时显示.禁用代理,问题就消失了.

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.

为此,我已向Apple提交了雷达报告.这是一个极端的情况,因为大多数最终用户不会在其设备上使用代理,但是开发人员通常使用代理来调试网络呼叫,并且使用NSURLSession并具有代理集的任何人都会看到此泄漏.在Openradar中重复出现的问题: https://openradar.appspot.com/radar?id=6140533516795904

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

这篇关于NSURLSession周围的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆