2011-10-12 127 views
1

我在XCode 4中得到Instrument工具報告ASIHttpRequest發生內存泄漏......我無法弄清楚問題所在,把我的所有代碼都處理完了,然後像下面那樣創建函數,但是xcode仍然報告相同的內存泄漏...儀器(XCode4)報告ASIHttpRequest泄漏內存?

每次點擊按鈕時都會調用此方法,並且每次點擊按鈕時都會發生更多內存泄漏。 :(

- (void) loadData 
{ 
    // no data set, we need to load ourself 
    NSURL *url = [self getDataUrl]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 

    //////////////////////////////////////////////////////////////////// 
    // set cache policy 
    // 

    // always store data in cache 
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
    // Always ask the server if there is new content available, 
    // If the request fails, use data from the cache even if it should have expired. 
    [request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy]; 


    [request setCompletionBlock:^{ 
     NSLog(@"[http request] finishing %@", url); 

     [self dataDidLoadSuccess]; 
    }]; 

    [request setFailedBlock:^{ 
     NSError *error = [request error]; 
     NSLog(@"[http request]Failed to perform request to %@: %@", url, error); 
     [self dataDidLoadFail:error]; 
    }]; 

    [request startAsynchronous]; 
} 

以下是從儀器複製檢測泄漏(只是一部分):

Leaked Object # Address Size Responsible Library Responsible Frame 
__NSMallocBlock__,2 <multiple> 64 Bytes UIKit -[UIViewController view] 
NSCFString,2 <multiple> 64 Bytes CFNetwork HTTPMessage::parseHeadersFromData() 
GeneralBlock-16,2 <multiple> 32 Bytes Foundation -[NSThread main] 
NSRecursiveLock,2 <multiple> 160 Bytes Foundation +[NSRecursiveLock allocWithZone:] 
NSConcreteMutableData,2 <multiple> 64 Bytes Foundation +[NSMutableData(NSMutableData) allocWithZone:] 
__NSArrayM,2 <multiple> 64 Bytes UIKit -[UIViewController view] 
__NSMallocBlock__,2 <multiple> 64 Bytes UIKit -[UIViewController view] 
__NSArrayM,2 <multiple> 64 Bytes Foundation +[NSHTTPCookie _cf2nsCookies:] 
__NSOperationInternal,2 <multiple> 288 Bytes Foundation -[NSOperation init] 
NSCFString,  0xb35fdc0 16 Bytes CFNetwork createCapitalizedHeaderString 
NSCFString,  0xb35fda0 32 Bytes CFNetwork HTTPMessage::extractResponseStatusLine(unsigned char const*, long) 
GeneralBlock-32, 0xb35cd10 32 Bytes CFNetwork HTTPMessage::internalSetHeader(__CFString const*, __CFString const*, long) 
__NSCFArray, 0xb35c550 32 Bytes CFNetwork HTTPReadStream::streamEvent(unsigned long) 
GeneralBlock-48, 0xb35c520 48 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*) 
GeneralBlock-16, 0xb35c440 16 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*) 
__NSCFInputStream, 0xb35c420 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*) 
GeneralBlock-32, 0xb35ba80 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*) 
__NSCFArray, 0xb35ba60 32 Bytes CFNetwork HTTPReadStream::constructProxyList(CFStreamError*) 
GeneralBlock-48, 0xb35ba10 48 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*) 
CFHTTPMessage, 0xb35b950 80 Bytes CFNetwork HTTPReadStream::streamOpen(__CFReadStream*, CFStreamError*, unsigned char*) 
GeneralBlock-48, 0xb35b920 48 Bytes Foundation -[NSThread main] 
__NSCFArray, 0xb35b900 32 Bytes CFNetwork HTTPMessage::initialize(HTTPMessage*) 
__NSCFArray, 0xb35b8e0 32 Bytes Foundation -[NSThread main] 
__NSCFArray, 0xb35b8c0 32 Bytes CFNetwork HTTPReadStream::startRequest(CFStreamError*) 
GeneralBlock-48, 0xb35b610 48 Bytes Foundation -[NSThread main] 
GeneralBlock-16, 0xb35b5f0 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*) 
GeneralBlock-32, 0xb35b5d0 32 Bytes Foundation -[NSThread main] 
GeneralBlock-32, 0xb35b5b0 32 Bytes Foundation -[NSThread main] 
NSCFString,  0xb35b590 32 Bytes Foundation -[NSURL(NSURL) host] 
GeneralBlock-16, 0xb35b570 16 Bytes CFNetwork HTTPReadStream::streamSetProperty(__CFReadStream*, __CFString const*, void const*) 
__NSCFDictionary, 0xb35b540 48 Bytes Foundation -[NSThread main] 
__NSCFDictionary, 0xb35b490 48 Bytes CFNetwork 

Screenshot

+0

後scrrenshot ... – iAmitWagh

+0

附截圖,我用手動快照,快照後,每次我調用「loadData」。 –

回答

8

我不知道,如果這是你的問題有或沒有,但要報價http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks

__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 

注意當我們聲明請求時使用__block限定符,這個很重要, !它告訴該塊不保留該請求,其中 在防止保留週期中很重要,因爲請求將始終保留該塊。

所以嘗試添加__block預選賽,並重新測試,看看您是否仍然有問題...儀器屏幕的

+0

非常感謝!它是固定的!通過添加__block,所有問題都消失了! –

+0

再一次,我應該RTFM :( –