NSURLConnection 泄漏? [英] NSURLConnection leak?

查看:22
本文介绍了NSURLConnection 泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个 nsurl,它从 http 中获取数据.当我运行仪器时,它说我有一个泄漏的 NSFNetwork 对象.

i have set up a nsurl which grabs the data from http. when i run instrument, it says i have a leak NSFNetwork object.

以及如何在 (void)ButtonClicked 中释放 theConnection ?还是稍后发布?

and how do i release theConnection in (void)ButtonClicked? or it will be release later on?

- (void)ButtonClicked {
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:KmlUrl]
                                                cachePolicy:NSURLRequestUseProtocolCachePolicy
                                            timeoutInterval:20.0f];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // receivedData is declared as a method instance elsewhere
        NSMutableData *receivedData = [[NSMutableData data] retain];
        [self setKMLdata:receivedData];
    } else {
        // inform the user that the download could not be made
    }
}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    // receivedData is declared as a method instance elsewhere
    [KMLdata appendData:data];
    NSLog(@"didReceiveData");
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // release the connection, and the data object
    [connection release];
    [KMLdata release];
}


- (void)connection:(NSURLConnection *)connection  didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    [connection release];
    // receivedData is declared as a method instance elsewhere
    [KMLdata release];

}

推荐答案

我终于找到了答案.

上述代码中的错误(顺便说一下,这是SDK 文档) 不在内存管理代码中.自动释放是一种选择,手动释放是另一种选择.无论您如何处理 NSURLConnection 对象,使用 NSURLConnection 都会发生泄漏.

The error in the above code (which by the way is the near-exact sample from the SDK docs) is not in the memory management code. Autorelease is one option, manual release is another. Regardless of how you handle your NSURLConnection object, you get leaks using NSURLConnection.

首先,这是解决方案.只需将这 3 行代码直接复制到 connectionDidFinishLoading、didFailWithError 和释放 NSURLConnection 对象的任何其他位置即可.

First up, here is the solution. Just copy these 3 lines of code directly into connectionDidFinishLoading, didFailWithError and anywhere else you release the NSURLConnection object.

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

归功于 http://forums.macrumors 上的 mpramodjain.com/showthread.php?t=573253 代码.

Credit to mpramodjain on http://forums.macrumors.com/showthread.php?t=573253 for the code.

问题似乎是这样 - SDK 在 iPhone 上缓存请求和回复.即使您的 NSMutableURLRequest cachePolicy 设置为不从缓存加载回复.

The problem seems to be this – the SDK caches the requests and replies on the iPhone. Even it seems if your NSMutableURLRequest cachePolicy is set to not load the reply from the cache.

愚蠢的是,它似乎默认缓存了很多数据.我正在传输大量数据(分成多个连接)并开始收到内存警告,最后我的应用程序死了.

The silly thing is that it seems to cache a lot of data by default. I'm transmitting a lot of data (split into multiple connections) and started to get memory warnings, and finally my App died.

我们需要的文档在 NSURLCache(不是 NSURLConnection)中,它们声明:

The docs we need are in NSURLCache (not NSURLConnection), they state:

NSURLCache 实现了缓存对 URL 加载请求的响应将 NSURLRequest 对象映射到NSCachedURLResponse 对象.它是一个一个内存和一个的组合磁盘缓存.

NSURLCache implements the caching of responses to URL load requests by mapping NSURLRequest objects to NSCachedURLResponse objects. It is a composite of an in-memory and an on-disk cache.

提供方法来操作每个缓存的大小控制磁盘上要使用的路径用于缓存数据的持久存储.

Methods are provided to manipulate the sizes of each of these caches as well as to control the path on disk to use for persistent storage of cache data.

这三行具有完全破坏缓存的效果.将它们添加到我的应用程序后(GPS 日志),我的#living 对象数量保持稳定.

Those three lines have the effect of nuking the cache totally. After adding them to my App (GPS Log), my #living object count remains steady.

这篇关于NSURLConnection 泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆