I'm using AFNetworking to perform URL request and defining success/error blocks within NSOperation - so that's basically running asynchronous process within NSOperation.
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 Asynchronous methods in NSOperation).
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?
Call stack on that thread is:
objc_release
_destroy_helper_block
_Block_release
__destroy_helper_block
_Block_release
start_wqthread
Code is
- (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];
}
I only know this is easily done with the ASINetworkQueue: