title: Blocks within NSOperation
body: <p>I'm using AFNetworking to perform URL request and defining success/error blocks within NSOperation - so that's basically running asynchronous process within NSOperation. </p>
  
  <p>I understand the caveat behind this approach, being NSOperation would terminate prematurely before delegate methods are called and therefore have implemented one of the suggested solutions by running start() on the main thread (relevant post <a href="https://stackoverflow.com/questions/1596160/asynchronous-methods-in-nsoperation">Asynchronous methods in NSOperation</a>). </p>
  
  <p>So far that's all good, I can see that the order of execution is correct i.e. success block executes, done, then dealloc gets called. UNTIL there's this (system?) thread called __destroy_helper_block that is referencing success block in the NSOperation which at this stage has been dealloc'd. What exactly is this? Is AFNetworking holding reference to the block?</p>
  
  <p>Call stack on that thread is:</p>
  
  <p>objc_release<br/>
  _destroy_helper_block<br/>
  _Block_release<br/>
  __destroy_helper_block<br/>
  _Block_release<br/>
  start_wqthread<br/></p>
  
  <p>Code is</p>
  
  <pre><code>- (void) start {
      ...
      void (^successHandler)(NSURLRequest *, NSHTTPURLResponse*, NSXMLParser *) = ^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
      URLRequestParserDelegate *parserDelegate = [[URLRequestParserDelegate alloc]initWithChildDelegate:self];
  
          // child to handle connection success
          [self handleSuccess:request response:response];
  
          // parse xml response data
          [XMLParser setDelegate:parserDelegate];     
          [XMLParser parse];
          [parserDelegate release];
          [self finish];
      }; // EXC_BAD_ACCESS on this line (?)
  
      AFXMLRequestOperation *op = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request 
      success:successHandler failure:nil];
      [op start];
  }
  </code></pre>