gpt4 book ai didi

iphone - 递增 `static int` 会导致 SIGSEGV SEGV_ACCERR

转载 作者:行者123 更新时间:2023-12-02 15:54:40 24 4
gpt4 key购买 nike

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

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 请求之前通过运行循环。为此,请使用 performSelector:withObject:afterDelay:,并传递“0.0”作为延迟。

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

关于iphone - 递增 `static int` 会导致 SIGSEGV SEGV_ACCERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6179459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com