#47 new

Crash EXC_BAD_ACCESS when canceling requests

Reported by Peter | March 6th, 2010 @ 03:21 PM

Hi, I'm getting sporadic crashes when canceling requests.

It crashes in startRequest: http://skitch.com/steipetee/n2w5q/xcode

I am stopping all current operations via
[networkQueue cancelAllOperations];

Am I doing something wrong?

Comments and changes to this ticket

  • fmendez

    fmendez March 16th, 2010 @ 09:16 PM

    We're seeing this as well. We fixed it on our end by adding "[self setQueue:nil];" to the ASIHTTPRequest -(void)cancel method.
    ASINetworkQueue.m's dealloc method is essentially trying to do the same thing but the operations arrays looks like it's empty by the time we get to dealloc

  • Ben Copsey

    Ben Copsey March 17th, 2010 @ 04:48 PM


    Thanks for the report.

    I really need to write a test for this so I can reproduce and confirm the fix - if either of you can provide me with sample code to reproduce the crash, that would be great.



  • Ben Copsey

    Ben Copsey March 17th, 2010 @ 04:49 PM

    • Assigned user set to “Ben Copsey”
  • Ben Clayton

    Ben Clayton May 5th, 2010 @ 05:51 PM

    I've been having a similar problem. I have an NSNetworkQueue of requests, that can be dealloc'ed while running by a user action (e.g. cancel).
    I was having some nasty crashes sporadically - but I was able to trace it through to delegate methods on the queued NSHttpRequest objects in the NSNetworkQueue calling a dealloc'ed object. I fixed it by setting the delegates in the requests to nil in the dealloc method.

    e.g. NSNetworkQueue.m's dealloc method now reads..

    • (void)dealloc { NSLog(@"ASINetworkQueue dealloc"); //We need to clear the queue on any requests that haven't got around to cleaning up yet, as otherwise they'll try to let us know if something goes wrong, and we'll be long gone by then for (ASIHTTPRequest *request in [self operations]) {
        [request setQueue:nil];
      // BEN ADDED
      request.delegate = nil;
      } // BEN ADDED delegate = nil; // END ADDITION [userInfo release]; [super dealloc]; }

    I don't know if this was the cause of Peter's issue, but it helped me!

  • hyperlink

    hyperlink October 15th, 2010 @ 06:51 AM

    • Milestone order changed from “0” to “0”

    Hi Ben,

    First off, awesome library - I love it! I'm getting similar results using a queue with authentication and its crashing sporadically. I wasn't sure if this was related to this ticket as well:


    I have tracked it down to the similar spot but I am not canceling a queue at all. It just closes out on me. I have ran it with a proxy and I'm getting a response 200 from my last service call. Please let me know if you want me to file it as a new bug.

    This is the message I'm getting with NSZombieEnabled:

    *** -[ASINetworkQueue respondsToSelector:]: message sent to deallocated instance 0xef52870


  • Tom Whipple

    Tom Whipple November 1st, 2010 @ 08:54 PM

    I'm having a very similar problem, which seems to stem from the delegate object being de-allocated prior to the request completing. I've tried clearing the request's delegate property in the delegate's dealloc, and I just tried Ben Clayton's approach above. Neither are successful.

    (This is an iOS 4 environment.)


  • Tom Whipple

    Tom Whipple November 1st, 2010 @ 10:15 PM

    Ok, I fixed my problem by adding @synchronized calls to ASIHTTPRequest.m and updating the delegate's dealloc as shown below. The problem turned out to be that the delegate was getting dealloc'd as requestFished was running. I'm attaching my updates.

    -(void) dealloc {

    request.didFailSelector = nil;
    request.didFinishSelector = nil;
    request.delegate = nil;
    [request cancel];
    [request release];
        // ...
    [super dealloc];


  • hyperlink

    hyperlink November 4th, 2010 @ 06:12 PM

    Hi Tom,

    Thanks for your post. I'll give it as shot as I am also on iOS 4 and the previous solutions have not been successful.

  • hyperlink

    hyperlink November 10th, 2010 @ 05:35 AM


    So your version is an older version of the current (60 diffs) but I have added the additional code in the dealloc as well as added some @synchronized calls and I think it's only crashed on me once. I'm keeping a very close eye on it and will post back if anything new develops. Thanks again for your post.

  • Tom Whipple

    Tom Whipple December 15th, 2010 @ 01:00 AM

    This continues to be an issue, even with the latest code and using the clearDelegatesAndCancel method in the delegate's dealloc.

  • Tom Whipple

    Tom Whipple December 15th, 2010 @ 02:20 AM

    It seems that my current round of problems are tied to line 2657 of ASIHTTPRequest.m (within handleNetworkEvent:), version "v1.8-10 2010-11-24".

    This section deals with redirected URLs, and I'm downloading a bunch of redirected images. (Facebook profile pics)

    Typically one thread is crashed here and another is accessing the delegate in some way. The delegate is in the process of being deallocated.

    I'm guessing that the delegate's dealloc gets called, cancels the request, but the request calls "performOnThread" (or performOnMainThread) which adds the method to the run loop rather than executing immediately. Once the scheduled method runs, the delegate has already been deallocated.

    Investigation continues....

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Easy to use CFNetwork wrapper for HTTP requests, Objective-C, Mac OS X and iPhone