2011-12-02 3 views
1

이 코드의 분석 실행시 "잠재적 인 누수"메시지가 표시된다. . 단순한 UINavigationController가 /의 TableView 비트)"잠재적 누설 오류"- 그러나 나는 그것을 볼 수 없다.

내가 얻을 전체 메시지는 다음과 같습니다

그것은 나에게 이해가되지 않습니다 " 'tempKey'할당에 저장된 객체의 잠재적 인 누출"- 누구나 볼 수 있습니다 그것?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // create a tempKey String var, which will store the clicked-artist's name 

    // -- this here is the line the compiler says the error is in: 
    NSString *tempKey = [[NSString alloc] init]; 


    if ([ArtisticStaffNames objectAtIndex:indexPath.row] == @"Jeff Smith") 
     tempKey = @"Jeff"; 
    else if ([ArtisticStaffNames objectAtIndex:indexPath.row] == @"Dan Jones") 
     tempKey = @"Dan"; 
    else if ([ArtisticStaffNames objectAtIndex:indexPath.row] == @"Matt Low") 
     tempKey = @"Mat"; 
    else if ([ArtisticStaffNames objectAtIndex:indexPath.row] == @"Lisa Jennings") 
     tempKey = @"Lis"; 
    else if ([ArtisticStaffNames objectAtIndex:indexPath.row] == @"Michael Bluarique") 
     tempKey = @"Mike"; 

    artisticStaffDetailVC *artStaffVC = [[artisticStaffDetailVC alloc] initWithNibName: @"artisticStaffDetailVC" bundle:nil]; 
    artStaffVC.key = tempKey; 

    [tempKey release]; 

    // Sets the text of the BACK button on next screen to "back": 
    // alloc a UIBarButtonItem: 
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init]; 
    backButton.title = @"Staff"; 

    self.navigationItem.backBarButtonItem = backButton; 
    [backButton release]; 

    // Pushes the next view/screen on: 
    [self.navigationController pushViewController:artStaffVC animated:YES]; 
    [artStaffVC.key release]; 

} 

답변

4

분석기가 정확합니다. 이렇게하면 :

NSString* someString = [[NSString alloc] init]; 

소유하고있는 NSString에 대한 포인터가 있습니다. 그런 다음이 작업을 수행 할 경우

someString = @"Blah"; 

당신은 새로운는 NSString 객체를 가리 키도록 someString을 할당하고, 제 1 유출했다. 이 줄은 단순히 기존 문자열의 내용을 변경하는 것이 아닙니다. 이것은 정확히 tempKey으로 무엇을하고 있는지를 보여줍니다.

+0

음 '단순히' 'tempKey'를 'alloc'ing하지 않고 선언 해 보았습니다. (의미 : 'NSString * tempKey;'- 'alloc'또는 'init'없음.)하지만 몇 줄 후에 'artStaffVC.key = tempKey;' "할당 된 값은 가비지 또는 정의되지 않았습니다."라는 메시지가 나타납니다. 그래서 나는 이것이 올바른 접근법이 아니라고 생각합니다. 그래서 .... 어떻게 해결할 수 있을까요? 이 작업을 수행하는 올바른 방법은 무엇입니까? – sirab333

+0

@ sirab333이라면 포인터를 단순히 선언하는 대신 nil로 초기화하는 것이 가장 좋습니다. 이것은 할당하지 않고 문자열을 사용하려고하면 이론적으로'EXC_BAD_ACCESS'를 막을 것입니다. NSString * tempKey = [[NSString alloc] init]; 대신'NSString * tempKey = nil;'을 사용하십시오. –

+0

그게 효과가! :-) 이제는 왜, 어떻게 :-) 도움을 주셔서 감사합니다. – sirab333

0

당신이 정말로 어디서 찾을 누출을 갖는되어 있는지 Instruments라는 도구를 사용합니다.

관련 문제