0

我的应用程序有一个按钮,用于检查输入的值是否正确。有时它会导致它崩溃,但奇怪的是它以不规则的间隔发生(有时在第三次迭代,有时在第十次,有时从不)。

我在调试器中收到 EXC_BAD_ACCESS 错误。所以似乎有些东西在不应该的时候被释放了。按钮调用此函数:

- (IBAction)checkValue:(id)sender{
int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];
actualDifferenceAsString = [NSString stringWithFormat:@"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
    UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:@"matches"
            message:@"next value."
            delegate:nil
            cancelButtonTitle:@"ok" 
            otherButtonTitles: nil];
    [correctAlert show];
    [correctAlert release];
}
else
{
    UIAlertView *incorrectAlert = [[UIAlertView alloc]
    initWithTitle:@"does not match"
            message:@"next value."
        delegate:nil
            cancelButtonTitle:@"ok"
            otherButtonTitles: nil];
    [incorrectAlert show];
    [incorrectAlert release];
}

使用僵尸指向第一个语句:

int actualDifference = [firstNumberString intValue] - [secondNumberString intValue];

有谁知道问题可能是什么?

4

2 回答 2

0

把它改成

 NSInteger actualDifference = [firstNumberString intValue] - [secondNumberString intValue];  //change int to NSInteger
NSString *actualDifferenceAsString = [NSString stringWithFormat:@"%d", actualDifference];
if ([answerTextField.text isEqualToString:actualDifferenceAsString])
{
    UIAlertView *correctAlert = [[UIAlertView alloc] initWithTitle:@"matches"
                                                           message:@"next value."
                                                          delegate:nil
                                                 cancelButtonTitle:@"ok" 
                                                 otherButtonTitles: nil];
    [correctAlert show];
    [correctAlert release];
}
else
{
    UIAlertView *incorrectAlert = [[UIAlertView alloc]
                                   initWithTitle:@"does not match"
                                   message:@"next value."
                                   delegate:nil
                                   cancelButtonTitle:@"ok"
                                   otherButtonTitles: nil];
    [incorrectAlert show];
    [incorrectAlert release];
}

你得到这个代码。它对我有用...

于 2012-04-12T09:51:24.860 回答
0

如果在第一行检测到僵尸,这意味着程序的其他部分正在释放firstNumberStringsecondNumberString. 这就是问题开始的地方,但它只出现在这里,当您稍后尝试访问这些值时。你还在哪里使用这些字符串?你有没有释放过它们?

为了整体安全,可能应该为它们分配属性,而不是成员变量。

于 2012-04-12T12:22:49.563 回答