本文介绍了NSURLSession内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
即使在NSURLSession无效,使用Instruments运行配置文件之后,一些名为TubeManager的类(可能是私有),HTTPConnectionCache和HTTPConnectionCacheDictionary仍然在内存中存在。
Even after invalidate a NSURLSession, running a profile using Instruments, some classes (probably privates) called TubeManager, HTTPConnectionCache and HTTPConnectionCacheDictionary still alive in memory.
代码片段到再现:
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
NSURLSessionDataTask* sessionDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
[session finishTasksAndInvalidate];
}];
[sessionDataTask resume];
推荐答案
在错误的位置调用finishTasksAndInvalidate ...
completionHandler用于处理响应它与会话无关
finishTasksAndInvalidate called in wrong place...completionHandler is for handling response it has nothing to do with session
这是正确的代码:
NSURLSessionConfiguration* config = [NSURLSessionConfigurationdefaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
NSURLSessionDataTask* sessionDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// handle response...
}];
[sessionDataTask resume];
[session finishTasksAndInvalidate];
这篇关于NSURLSession内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!