English 中文(简体)
got EXC_BAD_ACCESS when I Cancel a Asynchronous web request by using ASIHTTPRequest
原标题:

I have no problem to run the sample code below in a iPhone simulator, but when I run it in a iPhone , I always get a EXC_BAD_ACCESS when I call [asiRequest cancel]. anyone can help? thanks.

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;
}

After checking the api, I think I shouldn t release it in requestFinished or requestFailed

by how can I release it when it finishs?

- (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];
}
最佳回答

You need to retain your ASIHTTPRequest. Because the request was autoreleased via the use of a convenience constructor, you must retain it for it to stick around passed the end of the current run loop.

In addition, do note that you don t need to check if asiRequest is not nil in cancel1:: sending a message to nil does nothing, and has no adverse effects.

问题回答

I changed my code again

it works fine now, I think the only thing I need to worry about is I need release the request somethere when requestFailed happens unexpectedly

-(IBAction)request1{
    NSLog(@"request starting");
    [self sendRequest];
}
-(IBAction)cancel1{
    NSLog(@"request caceling");
    if(asiRequest){
        [asiRequest cancel];
        [asiRequest release];
        asiRequest=nil;
    }
}

-(void)sendRequest{

    asiRequest=[[ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://maps.google.com/"]] retain];
    [asiRequest setDelegate:self];
    [asiRequest startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished");
    [asiRequest release];
    asiRequest=nil;
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSLog(@"request Error=%@",[request error]);
}




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签