Instrument (XCode4) report ASIHttpRequest leak mem

2019-04-15 05:45发布

问题:

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

回答1:

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

Note the use of the __block qualifier when we declare the request, this is important! It tells the block not to retain the request, which is important in preventing a retain-cycle, since the request will always retain the block.

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