我在 iPhone 模拟器中运行下面的示例代码没有问题,但是当我在 iPhone 中运行它时,当我调用 [asiRequest cancel] 时,我总是得到一个 EXC_BAD_ACCESS。有人可以帮忙吗?谢谢。
ASIHTTPRequest *asiRequest;
-(IBAction)request1{
NSLog(@"request starting");
[self sendRequest];
}
-(IBAction)cancel1{
NSLog(@"request caceling");
if(asiRequest)
[asiRequest cancel];
}
-(void)sendRequest{
asiRequest=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/"]];
[asiRequest setDelegate:self];
[asiRequest startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished");
asiRequest=nil;
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSLog(@"request Error=%@",[request error]);
asiRequest=nil;
}
检查api后,我认为我不应该在requestFinished或requestFailed中释放它
完成后如何释放它?
- (void)cancel
{
#if DEBUG_REQUEST_STATUS
NSLog(@"Request cancelled: %@",self);
#endif
[[self cancelledLock] lock];
if ([self isCancelled] || [self complete]) {
[[self cancelledLock] unlock];
return;
}
[self failWithError:ASIRequestCancelledError];
[self setComplete:YES];
[self cancelLoad];
[[self cancelledLock] unlock];
// Must tell the operation to cancel after we unlock, as this request might be dealloced and then NSLock will log an error
[super cancel];
}