title: Avoiding EXC_BAD_ACCESS when using the delegate pattern
body: <p>A have a view controller, and it creates a "downloader" object, which has a reference to the view controller (as a delegate). The downloader calls back the view controller if it successfully downloads the item. This works fine as long as you stay on the view, but if you navigate away before the download is complete I get <code>EXC_BAD_ACCESS</code>. I understand why this is happening, but is there any way to check if an object is still allocated?
  I tried to test using <code>delegate != nil</code>, and <code>[delegate respondsToSelector:]</code>, but it chokes.</p>
  
  <pre><code>if (!self.delegate || ![self.delegate respondsToSelector:@selector(downloadComplete:)]) {
    // delegate is gone, go away quietly
          [self autorelease];
          return;
      }
  else {
    // delegate is still around
    [self.delegate downloadComplete:result];
  }
  </code></pre>
  
  <p>I know I could,</p>
  
  <p>a) have the downloader objects retain the view controller</p>
  
  <p>b) keep an array of downloaders in the view controller, and set their delegate values to nil when I deallocate the view controller.</p>
  
  <p>But I wonder if there is an easier way, where I just test if the delegate address contains a valid object?</p>