ASIHTTPRequest, EXC_BAD_ACCESS when request did finished

二次信任 提交于 2019-12-19 09:52:58

问题


I'm trying to do an asynchronous request with ASIHTTPRequest, but have some problem getting notified when the request is done.

-(void)doDownload{
    NSURL *url = [NSURL URLWithString:@"http://www.someurl.com/?"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:@"someValue" forKey:@"someField"];
    [request setRequestMethod:@"POST"];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestFinished)];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *responseString = [request responseString];

}

requestFinished is never called. I get an exception in ASIHTTPRequest.m, -handleStreamCompleted:

if (fileError) {
    [self failWithError:fileError];
} else {
    [self requestFinished];   <----- this call fails
}

Any clues?


回答1:


Are you sure that your class that implements - (void)requestFinished:(ASIHTTPRequest *)request is still there when the request finishes? It looks to me like the class gets deallocated too early. Note that the delegate property does not retain its content.

You could add a [self retain] to doDownload and a [self release] to - (void)requestFinished:(ASIHTTPRequest *)request, but make sure (!) that [self release] doesn't get called too often. This is also a possible memory leak if a request would never finish. It would be best to retain your class somewhere else.

You might also try to debug with NSZombieEnabled set to YES to find the error.




回答2:


Following line of your code seems to be wrong.

[request setDidFinishSelector:@selector(requestFinished)];

requestFinished method has an argument (ASIHTTPRequest *).
Therefore you should add ":", when you set a selector like as follows.

[request setDidFinishSelector:@selector(requestFinished:)];



回答3:


[request responseString];

Check retainCount of request before this call. Propably it equals zero :) If that - you shouldn't forget to retain it when you creates it in doDownload method.



来源:https://stackoverflow.com/questions/2164948/asihttprequest-exc-bad-access-when-request-did-finished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!