2012-02-23 67 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