title: Using NSAutoreleasePool with NSURLConnection
body: <p>I am trying to follow the XMLPerformance example to make an xml parser of my own.  So far I'm having the hardest time making autorelease pools work, I get a crash the instant I recreate a pool.</p>
  
  <p>I narrowed down the issue to this test case:</p>
  
  <h2>PoolCrashTest.h</h2>
  
  <pre><code>#import &lt;SenTestingKit/SenTestingKit.h&gt;
  
  @interface PoolCrashTest : SenTestCase
  {
    @private
    NSURLConnection *connection;
    NSAutoreleasePool *downloadAndParsePool;
    BOOL done;
  }
  
  @property (nonatomic, retain) NSURLConnection *connection;
  @property (nonatomic, assign) NSAutoreleasePool *downloadAndParsePool;
  
  - (void)downloadAndParse:(NSURL *)url;
  @end
  </code></pre>
  
  <h2>PoolCrashTest.m</h2>
  
  <pre><code>#import "PoolCrashTest.h"
  
  @implementation PoolCrashTest
  
  @synthesize downloadAndParsePool, connection;
  
  - (void)downloadAndParse:(NSURL *)url {
    done = NO;
    self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
    self.connection = [[NSURLConnection alloc] 
                       initWithRequest:theRequest delegate:self];
    if (connection != nil) {
      do {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                                 beforeDate:[NSDate distantFuture]];
      } while (!done);
    }
    self.connection = nil;
    [downloadAndParsePool release];
    self.downloadAndParsePool = nil;
  }
  
  #pragma mark NSURLConnection Delegate methods
  
  - (void)connection:(NSURLConnection *)connection 
      didReceiveData:(NSData *)data {
    [downloadAndParsePool drain];
  </code></pre>
  
  <p><strong>crash after this line ^</strong></p>
  
  <pre><code>  self.downloadAndParsePool = [[NSAutoreleasePool alloc] init];
  }
  
  - (void)testPoolCrash
  {
    NSURL *dumpURL = [NSURL URLWithString:@"file:///some.xml"];
  
    [NSThread detachNewThreadSelector:@selector(downloadAndParse:) 
                                 toTarget:self withObject:dumpURL];
    sleep(10);
  }
  
  @end
  </code></pre>
  
  <p>Can someone explain how to properly purge autorelease pool in a NSURLConnection delegate running in a thread?</p>
  
  <p>I have tried to follow XMLPerformance as close as possible...  I'm targeting Lion with mostly default project settings.</p>