NSURLConnection泄漏?

vic*_*cky 9 memory-leaks memory-management objective-c nsurlconnection nsurlrequest

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

我如何释放theConnection在(无效)ButtonClicked?或者它会在稍后发布?

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

}
Run Code Online (Sandbox Code Playgroud)

Wil*_*iss 17

我终于找到了答案.

上面的代码中的错误(顺便提一下,它是来自SDK文档的近乎精确的样本)不在内存管理代码中.自动释放是一种选择,手动释放是另一种选择.无论您如何处理NSURLConnection对象,都会使用NSURLConnection进行泄漏.

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

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
Run Code Online (Sandbox Code Playgroud)

感谢mpramodjainhttp://forums.macrumors.com/showthread.php?t=573253的代码.

问题似乎是这个 - SDK缓存iPhone上的请求和回复.即使看起来你的NSMutableURLRequest cachePolicy被设置为不加载来自缓存的回复.

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

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

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

提供了用于操纵每个高速缓存的大小以及控制磁盘上的路径以用于高速缓存数据的持久存储的方法.

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

  • 如果你不想完全禁用缓存而不能或不想使用与委托的异步连接,你可以在发送请求后调用`[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];` (2认同)

Pix*_*man 5

您好,您是否测试过此委托方法?

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

您可以更精确地管理缓存.

"重置"NSURLCache*sharedCache可能会导致代码的其他部分出现问题?


h4x*_*xxr 2

这是一个常见的问题,可以通过[object autorelease]的魔力来解决。在您的代码中,如下所示:

NSURLConnection *theConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
Run Code Online (Sandbox Code Playgroud)

这样,该对象会自动添加到“自动释放池”中,并在不再被引用后在下一个运行循环开始时释放。

希望有帮助

编辑:另外,我不明白为什么你需要在receivedData变量上调用-retain。