我正在调试报告为的崩溃:

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR

崩溃发生在执行numberOfFails++的行上。

该应用程序使用ASIHTTP。我个人更喜欢使用NSURLConnection。如果失败,我将永远不会自动重复对NSURLConnection的请求,因为我从未见过在不应该失败的情况下会失败。我宁愿给UI一个刷新按钮,或者显示一个带有按钮的UIAlertView再试一次或类似的操作。

无论如何,为了与其他团队成员合作,我正在寻求解决此问题,而目前暂时不将ASIHTTP替换为NSURLConnection

该请求正在以类似以下的内容开始:
- (void)getResources:(CLLocation *)location withQuery:(NSString *)query {
    NSURL *url = [NSURL URLWithString:[NSString stringWithString:@"https://example.com/"]];
    self.resourcesAPIRequest = [ASIFormDataRequest requestWithURL:url];
    [resourcesAPIRequest setPostValue:[Model instance].oauth_token forKey:@"oauth_token"];
    [resourcesAPIRequest setPostValue:[[NSNumber numberWithDouble:location.coordinate.latitude] stringValue] forKey:@"latitude"];
    [resourcesAPIRequest setPostValue:[[NSNumber numberWithDouble:location.coordinate.longitude] stringValue] forKey:@"longitude"];
    [resourcesAPIRequest setPostValue:query forKey:@"query"];
    [resourcesAPIRequest setDelegate:self];
    [resourcesAPIRequest setDidFinishSelector:@selector(resourcesAPIReturned:)];
    resourcesAPIRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:NSStringFromSelector(@selector(getResources:withQuery:)), @"repeatSelector", location, @"argument1", query, @"argument2", nil];
    [resourcesAPIRequest startAsynchronous];
}

我注意到的一件事是: <ASIHTTPRequestDelegate>不在头文件中,但是此回调方法仍称为OK:
#define maximumNumberOfFails 50

- (void)requestFailed:(ASIFormDataRequest *)request {
    static int numberOfFails = 0;

    if (numberOfFails < maximumNumberOfFails) {
        [NSThread sleepForTimeInterval:sleepTimeInSeconds];
        if ([request.userInfo objectForKey:@"argument2"]) {
            [self performSelector:NSSelectorFromString([request.userInfo objectForKey:@"repeatSelector"]) withObject:[request.userInfo objectForKey:@"argument1"] withObject:[request.userInfo objectForKey:@"argument2"]];
        } else if ([request.userInfo objectForKey:@"argument1"]) {
            [self performSelector:NSSelectorFromString([request.userInfo objectForKey:@"repeatSelector"]) withObject:[request.userInfo objectForKey:@"argument1"]];
        } else {
            [self performSelector:NSSelectorFromString([request.userInfo objectForKey:@"repeatSelector"])];
        }
        numberOfFails++;
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Problem" message:@"There was a problem connecting to the servers.  Please make sure you have an Internet connection." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
        numberOfFails = 0;
    }
}

另外,我认为static int numberOfFails应该是static NSUInteger numberOfFails。而且,我注意到请求是从startAsynchronous开始的。 static int numberOfFails是原子的吗?这可能就是为什么我们收到错误SEGV_ACCERR(映射对象的无效权限)的原因。

有什么想法吗?

最佳答案

该问题可能与您的静态变量无关。
requestFailed:是在主线程上还是在后台线程上执行?

如果它在后台线程上,则需要使用performSelectorOnMainThread:withObject:

如果它在主线程上,则可能需要在执行新的HTTP请求之前通过runloop。为此,请使用performSelector:withObject:afterDelay:,并将“0.0”作为延迟。

您会注意到,这两种方法都只允许使用一个方法参数。通常,您以NSDictionary传递参数,而不是像执行操作那样尝试事先解析出参数数量。

10-07 18:41