2012-04-12 2 views
0

입력 한 값이 올바른지 확인하는 버튼이 내 앱에 있습니다. 가끔 충돌이 발생하지만 이상한 점은 불규칙한 간격으로 발생한다는 것입니다 (때로는 세 번째 반복에서, 때로는 10 번째에서 가끔 발생하는 경우도 있음).EXC_BAD_ACCESS 오류 - 가끔만

디버거에서 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]; 

사람이 문제가 무엇인지 알고 있나요?

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]; 
} 

+0

exc_bad_access를 얻을 때 강조 표시되는 행은 무엇입니까? – Manuel

+1

중단 점을 사용하여 디버깅을 했습니까? 앱이 충돌하는 줄을 확인 하시겠습니까? firstNumberString, secondNumberString, actualDifferenceAsString – mChopsey

+0

의 인쇄 값은 강조 표시되지 않습니다. firstNumberString과 secondNumberString의 값은 arc4random()을 사용하는 다른 메소드에서 무작위로 설정됩니다. –

답변

0

좀비가 첫 번째 줄에서 발견되면 프로그램의 다른 부분이 firstNumberString 또는 secondNumberString을 릴리스하고 있음을 의미합니다. 그것이 문제가 시작되는 곳이지만, 나중에 그 값에 액세스하려고 할 때만 나타납니다. 그 끈으로 다른 곳에서 일하십니까? 너는 그들을 풀어 준 적 있니?

전반적인 안전을 위해 멤버 변수가 아닌 속성이 할당되어 있어야합니다.

+0

예. 헤더 파일의 등록 정보로 지정됩니다. 나는 그들에게 다른 방법으로 무작위 값을 할당한다. 그 문자열을 릴리스하는 유일한 시간은 dealloc() 메서드에 있습니다. viewDidUnload()에서 nil로 설정합니다. –

+0

'firstNumberString'과'secondNumberString'을 속성으로 정의 할 수 있습니다.하지만 여기서 변수로 액세스하고 있습니다. 속성을 읽으려면'self.firstNumberString' 또는'[self firstNumberString]'을 사용하고, 값을 설정하는 곳에서는'self.firstNumberString ='또는'[self setFirstNumberString :]'을 사용하십시오. 자세한 내용은 질의 응답 [속성 특성 "retain"이 작동하지 않는 것 같습니까?] (0120-383-0981) – Dondragmer

+0

작동하는지 여부를 알기가 어려웠습니다. 몇 번의 반복 작업으로도 잘 돌아가서이 문제가 생겼을 때 갑작스럽게 끝날 것이기 때문에 그 문제를 해결했다고 생각합니다. 감사! 나는 비행 중에 객관적인 것을 배우고 있는데, 그것은 내가 예상했던 것보다 다른 언어들과 많이 다르다. 당신은 이런 개념을 배우기에 좋은 자원을 추천 할 수 있습니까? 어쩌면 강력한 목표 -C 펀더멘털 책일까요? 다시 한 번 감사드립니다! –

0

변경 당신이 코드를 얻을. 그것은 나를 위해 일했습니다 ...

+0

'NSInteger'는'typedef int'입니다 (iOS 5.1 현재). 따라서이 변경 사항은 아무런 효과가 없습니다. 이 코드는 문제가 없으며 수정이 필요하지 않습니다. 다른 곳의 메모리 관리 때문에 응용 프로그램이 충돌합니다. – Dondragmer

+0

오른쪽. 나는 이미 그것을 시도하고, 그것은 여전히 ​​갑자기 끝납니다. –

관련 문제