RestKit/RestKit — pull request #872 by cfis (2012-07-21 00:50:38+00:00)
Title: Fix segmentation fault when requests are cancelled

1. Load a number of requests (more than one)
2. Have them all get cancelled because the network connection gets cancelled
3. Now get a segmentation fault.

The segmentation fault is caused by recyling the cancelled request - it gets sent a second time. The reason for this is that the RKObjectLoader#finalizeLoad sets self.loading to false and self.loaded to false. But it doesn't actually remove the request from the queue, that is done via a notification that is posted to the main thread, so there is a fair bit of delay.

As a result, the next time nextInQueue is called by the RKRequestQueue, nextInQueue may return the cancelled request. That causes the request to be sent again, resulting in a segmentation fault because in the meantime the request will be deallocated. So when a NSURLConnection callback occurs, RKResponse#request is a freed object.

Note that RKRequestQueue#processRequestDidLoadResponseNotification, which is called on a request failing, removes the request from the loading array but not the request array. This kind of looks like old code to me, not sure there is any point in calling [self removeLoadingRequest:request] anymore (it doesn't break things...just led me down the wrong track for a while).