Ios5 异步连接泄漏
我知道这个问题已经被报道了很多次,但几乎所有的答案都与非ARC项目有关,我已经被困了好几天了。我在很多论坛和文章中都看过。我希望我能在这里找到一些帮助 我使用异步NSURLConnection请求,以便使用IOS 5和ARC下载图片,以下代码来源于《学习IPAd编程》一书:Ios5 异步连接泄漏,ios5,memory-leaks,automatic-ref-counting,nsurlconnection,nsurlconnectiondelegate,Ios5,Memory Leaks,Automatic Ref Counting,Nsurlconnection,Nsurlconnectiondelegate,我知道这个问题已经被报道了很多次,但几乎所有的答案都与非ARC项目有关,我已经被困了好几天了。我在很多论坛和文章中都看过。我希望我能在这里找到一些帮助 我使用异步NSURLConnection请求,以便使用IOS 5和ARC下载图片,以下代码来源于《学习IPAd编程》一书: -(void) dowloadImageAtURL:(NSURL *)URL { if (URL) { self.image = nil; self.recei
-(void) dowloadImageAtURL:(NSURL *)URL
{
if (URL)
{
self.image = nil;
self.receivedData = [[NSMutableData alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request // Building asynchronous connection
delegate:self
startImmediately:NO]; // This is the key to not run the connection synchronous
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; // We use the current loop but it will still be asynchronous
// because of the mode added to the current loop
[connection start];
request = nil;
}
}
#pragma marks - NSURLConnection delegate Methods
/*
Called when the web server responds to the request.
When the method is called, the receivedData property is reset with a length of zero, clearing any previously stored data.
This ensures that only the data received from the final request is captured (see NSURLConnectionDelegate reference).
*/
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.receivedData setLength:0];
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
connection = nil;
}
/*
Called when data is received from the network. The method can be called a multiple times during a single request.
The data is appended to the data already stored in the receivedData property.
*/
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
/*
Called after the request has completed all data and all data has been received. This method converts
the data stored to a UIImage object.
*/
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
self.image = [UIImage imageWithData:self.receivedData];
self.receivedData = nil;
connection = nil;
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
// Here we post a notification to the observer
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.perfectmemory.famille.imageupdated" object:self];
}
/*
Called if an error is detected at any time during the download process.
*/
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
self.receivedData = nil;
connection = nil;
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
sharedCache = nil;
// Here we post a notification to the observer
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.perfectmemory.famille.imageupdated" object:self];
}
运行这段代码时,下载效果很好,但我可以看到XCode工具中存在漏洞
URLConnectionLoader::LoaderConnectionEventQueue
URLConnection::scheduleWithRunLoop
CFURLResponse
URLConnectionInstanceData
我已使用以下帮助尝试解决此问题,例如清除缓存,但没有成功:
我还尝试通过以下方式释放每个相关委托方法中的连接:
connection = nil
但仍然没有成功。我真的不明白发生了什么事。我找不到未发布的对象。你能帮帮我吗?这非常令人沮丧,因为我想这种异步下载请求非常常见
先谢谢你