增加`static int`会导致SIGSEGV SEGV_ACCERR

时间:2011-05-30 18:08:01

标签: iphone objective-c ios crash sigsegv

我正在调试崩溃报告为:

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(映射对象的无效权限)的原因。

思想?

1 个答案:

答案 0 :(得分:3)

问题可能与您的静态变量无关。

requestFailed:是在主线程上还是在后台线程中执行?

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

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

您会注意到这两种方法只允许一个方法参数。通常情况下,您将参数传递给NSDictionary,而不是尝试预先解析参数的数量,就像您正在做的那样。