2011-12-02 90 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"; 

已分配someString指向一個的NSString對象,並泄露了第一。該行不會簡單地更改現有字符串的內容。這正是您在使用tempKey時所要做的。

+0

嗯,我試着簡單地聲明'tempKey'沒有'分配它。 (含義:'NSString * tempKey;' - 沒有'alloc'或'init'。)但是之後幾行,在'artStaffVC.key = tempKey;'我得到這個消息:「分配的值是垃圾或未定義的」。所以我想這不是正確的做法。所以....我該如何解決這個問題?什麼是正確的方法來做到這一點? – sirab333

+0

@ sirab333,最好將指針初始化爲零,而不是簡單地聲明它。如果試圖在不分配它的情況下使用該字符串,這在理論上會阻止「EXC_BAD_ACCESS」。所以試試'NSString * tempKey = nil;'而不是'NSString * tempKey = [[NSString alloc] init];'。 –

+0

工作! :-)現在只需弄清楚爲什麼和如何:-)感謝您的幫助。 – sirab333

0

使用名爲Instruments看,如果你真的是有泄漏,在哪裏可以找到它的工具。