【问题标题】:Incrementing `static int` causes SIGSEGV SEGV_ACCERR递增 `static int` 会导致 SIGSEGV SEGV_ACCERR
【发布时间】:2011-05-30 18:08:01
【问题描述】:

我正在调试一个崩溃报告为:

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

想法?

【问题讨论】:

    标签: iphone objective-c ios crash segmentation-fault


    【解决方案1】:

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

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

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

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

    您会注意到这两种方法都只允许一个方法参数。通常,您在 NSDictionary 中传递参数,而不是像您正在做的那样尝试事先解析出参数的数量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多