Search code examples
iphoneobjective-cdownloadasihttprequestexc-bad-access

exc_bad_access from ASIHTTPRequest when popping view controller while downloading for iphone


I have a custom navigation controller so i made my own back button that does

-(void)navigateBack{
    [self.navigationController popViewControllerAnimated:YES];
}

and on the view did load, I request for many images using the ASINetworkQueue

-(void)sendRequest:(NSURL *)url withParams:(NSDictionary *)params andSelector:(NSString *)selector{
NSString *jsonString = [params yajl_JSONString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:jsonString forKey:@"json"];
[request setTimeOutSeconds:15];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidFinishSelector:NSSelectorFromString(selector)];
[[self networkQueue] setRequestDidFailSelector:@selector(asiRequestFailed:)];
[[self networkQueue] addOperation:request];
[[self networkQueue] go];
}

Essentially, anytime I send an HTTP request, i just do [self sendRequest:]. When I go to this viewcontroller, then press back before all the images are downloading, I get an EXC_BAD_ACCESS that points to the line where ASIHTTPFormRequest tries to perform the selector. I tried putting

[networkQueue cancelAllOperations];[self setNetworkQueue:nil];

in my dealloc method, and i put breakpoints before and after this, and I can see from the GDB that there are no more requests in the queue, but the error keeps showing. Am i missing something?

thanks


Solution

  • Your delegates will likely get called to notify you of the cancellation. You need to remove all the delegates in your dealloc method, eg:

    for (ASIHTTPRequest *req in [queue operations])
    {
        [req setDelegate:nil];
        [req cancel];
    }
    [queue setDelegate:nil];
    

    (and also progress delegates etc if you're using them)