本文介绍了仪器(XCode4)报告ASIHttpRequest泄漏内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XCode 4中得到了仪器工具报告来自ASIHttpRequest的内存泄漏...我无法弄清楚问题,转过来我们已经注释掉了我的所有代码来处理结果并使其功能如下,但是xcode仍然报告相同的内存泄漏......

I get Instrument tools in XCode 4 report memory leaks from ASIHttpRequest... I didn't be able to figure out the problem, turned our I commented out all my code to handle the result and make the function like below, but xcode still report the same memory leak...

每次单击按钮时都会调用此方法,每当我点击按钮时,我会看到更多的内存泄漏按钮。 :(

This method is called everytime when I click a button, and I will see more memory leak happen every time when I hit the button. :(

- (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];
}






从工具中复制以下内容检测到泄漏(仅一部分):


Following is copied from Instrument detected leaks (only a part):

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

推荐答案

我'我不确定这是你遇到的问题,而是引用:

I'm not sure if this is the problem you're having or not, but to quote http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks :

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



所以尝试添加__block限定符,然后重新测试,看看你是否还有问题......

So try adding the __block qualifier, and retest and see if you still have the problem...

这篇关于仪器(XCode4)报告ASIHttpRequest泄漏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:31