NSURLConnection泄漏? [英] NSURLConnection leak?

查看:88
本文介绍了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];

}

推荐答案

我终于找到了答案.

以上代码中的错误(顺便说一句,它是

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.

首先,这是解决方案.只需将这三行代码直接复制到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];

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天全站免登陆