仪器(XCode4)报告ASIHttpRequest泄漏内存?

发布于 2024-12-09 12:36:39 字数 4443 浏览 0 评论 0原文

我在 XCode 4 中得到 Instrument 工具报告来自 ASIHttpRequest 的内存泄漏...我无法找出问题,转而我注释掉了所有代码来处理结果并使函数如下所示,但 xcode 仍然报告同样的内存泄漏...

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

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

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

Screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-12-16 12:36:39

我不确定这是否是您遇到的问题,但引用 http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks

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

请注意在声明请求时使用 __block 限定符,
这很重要!它告诉块不要保留请求,这
对于防止保留周期很重要,因为请求将
始终保留该块。

因此,尝试添加 __block 限定符,然后重新测试,看看问题是否仍然存在......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文