title: Asynchronous NSURLConnection leaking
body: <p>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.</p>
  
  <p>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") :</p>
  
  <pre><code>-(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];
  
  }
  </code></pre>
  
  <p>When running this code, the download works well but I can see leaks in the XCode instruments</p>
  
  <ul>
  <li>URLConnectionLoader::LoaderConnectionEventQueue  </li>
  <li>URLConnection::scheduleWithRunLoop</li>
  <li>CFURLResponse</li>
  <li><p>URLConnectionInstanceData</p>
  
  <p>I have used the following helps to try to fix the issue, such as clearing the cache, but no success :</p></li>
  <li><a href="https://stackoverflow.com/questions/1697748/nsurlconnection-leaks-why">NSURLConnection Leaks -- Why?</a></li>
  <li><a href="http://www.friendlydeveloper.com/2010/04/successfully-working-around-the-infamous-nsurlconnection-leak/" rel="nofollow noreferrer">http://www.friendlydeveloper.com/2010/04/successfully-working-around-the-infamous-nsurlconnection-leak/</a></li>
  <li><a href="https://stackoverflow.com/questions/1345663/nsurlconnection-leak">NSURLConnection leak?</a></li>
  </ul>
  
  <p>I also tried to release the connection in each concerned delegate methods with :</p>
  
  <pre><code>connection = nil
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks a lot in advance.</p>