我创建了一个从Web下载图像的类。问题是它有内存泄漏。因为这已被多次调用。使用此方法调用下载的所有图像。由于图像来源无限,泄漏正在增加。
除了 main 之外,Stack Trace没有显示任何代码泄漏。
泄露对象: Malloc 144,176,128,160字节。
Resposible Library: CFNetwork
负责任框架: createCanonicalURL
堆栈追踪:
0 CFNetwork createCanonicalURL
1 CFNetwork HTTPProtocol::_createMutableCanonicalRequest(__CFAllocator const*, _CFURLRequest const*, void const*)
2 CFNetwork HTTPProtocol::_createCanonicalRequest(__CFAllocator const*, _CFURLRequest const*, void const*)
3 CFNetwork HTTPProtocol::copyCanonicalRequest()
4 CFNetwork URLConnectionLoader::copyProtocolCanonicalRequest()
5 CFNetwork URLConnectionClient::getRequestForTransmission(unsigned char, _CFURLResponse*, _CFURLRequest const*, __CFError**)
6 CFNetwork URLConnectionClient::_clientWillSendRequest(_CFURLRequest const*, _CFURLResponse*, URLConnectionClient::ClientConnectionEventQueue*)
7 CFNetwork URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long)
8 CFNetwork URLConnectionClient::processEvents()
9 CFNetwork MultiplexerSource::perform()
10 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
11 CoreFoundation __CFRunLoopDoSources0
12 CoreFoundation __CFRunLoopRun
13 CoreFoundation CFRunLoopRunSpecific
14 CoreFoundation CFRunLoopRunInMode
15 GraphicsServices GSEventRunModal
16 GraphicsServices GSEventRun
17 UIKit UIApplicationMain
18 Interior News main /iPhone Ongoing Projces/WAN_Interiors/Latest_Interior_News_V_1_2/Latest_Interior_News/main.m:14
19 Interior News start
正确释放连接对象。
这也是设备中的一部分。他们中的大多数人都说它是NSURLConnection中的一个错误,有人说有工作,有些人说不是
我尝试过设置最多提到
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
但没有用......
请咨询
代码
if (connectionForImage==nil)
{
NSString * fullImagePath = [rootURLForImage stringByAppendingPathComponent:imagePath];
NSString * trimedstring = [fullImagePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * percentEscapedUrl = [trimedstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Trimmed :%@",trimedstring);
NSLog(@"PercentEscaped %@",percentEscapedUrl);
NSURLRequest * imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:percentEscapedUrl] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:6.0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
connectionForImage =[[NSURLConnection alloc] initWithRequest:imageRequest delegate:self];
}
图像连接在连接完成加载和错误时被释放。
答案 0 :(得分:0)
如果是4.x及以上,只需打开你的xcode并点击选项+命令+ R.它将打开一个窗口,然后单击Add按钮并输入“NSZombieEnabled”并将值设置为YES。
答案 1 :(得分:0)
NSURLConnection
对于小泄漏有点臭名昭着。如果我正确地读取您的信息,它大概是半个kB。你确定这是你的记忆问题的原因吗?您可能需要仔细检查堆镜头,并确认您没有以其他方式累积内存。并非所有内存累积都会显示为泄漏。
根据我的经验,如果你使用NSURLConnection
,尤其是UIWebKit
,你通常会有小的(一次一百个字节)泄漏来应对。
答案 2 :(得分:0)
我尝试了ASIHTTPRequest然后遇到了一个badurl错误。我检查了网址似乎格式为http:/www.example.com而不是http://www.example.com后来发现代码
NSString * fullImagePath = [rootURLForImage stringByAppendingPathComponent:imagePath]; 是问题。
它是从http://中删除斜杠的那个 所以我的代码改为
NSString * fullImagePath = [rootURLForImage stringByAppendingPathComponent:imagePath];
NSString * trimedstring = [imagePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * percentEscapedUrl = [trimedstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * rootURL = [NSURL URLWithString:rootURLForImage];
NSURL * finalURL = [NSURL URLWithString:percentEscapedUrl relativeToURL:rootURL];
NSURLRequest * imageRequest = [NSURLRequest requestWithURL:finalURL cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:60.0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
connectionForImage =[[NSURLConnection alloc] initWithRequest:imageRequest delegate:self];