I know that issue has been reported tons of times but almost all the answers concerned non ARC projects and I have been stuck for days now. I have looked in many forums and articles. I hope I could find some help here.
I use an asynchronous NSURLConnection request in order to download pictures, with IOS 5, using ARC, as follow (code inspired from the book "Learning IPAd programming") :
-(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];
}
When running this code, the download works well but I can see leaks in the XCode instruments
- URLConnectionLoader::LoaderConnectionEventQueue
- URLConnection::scheduleWithRunLoop
- CFURLResponse
URLConnectionInstanceData
I have used the following helps to try to fix the issue, such as clearing the cache, but no success :
- NSURLConnection Leaks -- Why?
- http://www.friendlydeveloper.com/2010/04/successfully-working-around-the-infamous-nsurlconnection-leak/
- NSURLConnection leak?
I also tried to release the connection in each concerned delegate methods with :
connection = nil
But still no success. I really don't understand what is going on. I can't find unreleased objects. Could you please help me ? It's very frustrating since I guess this asynchronous download request is very common.
Thanks a lot in advance.