2012-02-23 41 views
1

我创建了一个从网上下载图像的类。问题是它有一个内存泄漏。因为这已被多次调用。所有下载的图像都使用此方法调用。由于有无限的图像来源,泄漏正在上升。NSURLConnection上的内存泄漏问题

堆栈跟踪没有显示代码泄漏,除了主要

泄漏对象: Malloc 144,176,128,160字节。

Resposible库: 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 

连接对象正常释放。

这也在Device中出现。他们大多表示,其在NSURLConnection的一个bug,有人说有工作araound ADN有人说没有

我已经尝试设置为最提到

[[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

显示你的代码...或启用NSZombie ...并运行... – Rams 2012-02-23 05:16:35

+0

NSZombieEnabled如何帮助跟踪泄漏 – 2012-02-23 05:38:13

+0

您正在使用哪个版本的xcode。 – iamsult 2012-02-23 05:42:55

回答

0

我试过ASIHTTPRequest thenI遇到badurl错误。我检查了这个URL它似乎是格式http:/www.example.com而不是http://www.example.com后来发现代码为

NSString * fullImagePath = [rootURLForImage stringByAppendingPathComponent:imagePath]; 是问题。

这是这是从http remoging斜线的一个:// 因此改变了我的代码

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

如果4.x及以上版本只是打开你的Xcode并点击选项+命令+ R.它会打开一个窗口,然后点击添加按钮并输入“NSZombieEnabled”并将值设置为YES。

+0

为什么打开'NSZombieEnabled'来调试泄漏?僵尸帮助你找到过度释放,而不是过度保留。 – 2012-02-23 05:47:36

+0

你能解释一下吗? NSZombieEnabled会故意创建泄漏(它会阻止您释放NSObject;这就是它的工作原理)。它是如何帮助你找到泄漏的?我从来没有听说过。 – 2012-02-23 06:05:27

+0

@RobNapier你是对的。我删除了我的评论。谢谢。 – iamsult 2012-02-23 06:11:17

0

NSURLConnection有点臭名昭着的小泄漏。如果我正确读取您的信息,它大约是半个kB。你确定这是你记忆力问题的原因吗?您可能希望仔细检查堆积,并确认您没有以其他方式累积内存。并非所有的记忆累积都会显示为泄漏。

以我的经验,如果你使用NSURLConnection,尤其是UIWebKit,你通常会有小的(一次100字节)泄漏来应对。

+0

没有任何解决的办法。 – 2012-02-23 07:19:26

+0

这里和那里不是几百个字节。但这通常不会产生重大影响(可可一直有很多小漏洞)。我会确保这确实是你记忆问题的原因(如果你有真正的记忆问题,而不仅仅是泄漏仪器上的几个点)。 – 2012-02-23 14:47:01