“潜在的泄漏错误” - 但我没有看到它

时间:2011-12-02 16:49:56

标签: iphone objective-c memory memory-leaks

在执行此代码的分析运行时,我收到“潜在泄漏”消息 - 顺便说一下,它没有错误或崩溃(它只是一个简单的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];

}

2 个答案:

答案 0 :(得分:4)

分析仪是正确的。如果你这样做:

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

您有一个指向您拥有的NSString的指针。如果你这样做:

someString = @"Blah";

您已指定someString指向 NSString对象,并泄露了第一个。该行不会简单地更改现有字符串的内容。这正是您对tempKey所做的一切。

答案 1 :(得分:0)

使用名为Instruments的工具查看您是否真的有泄漏以及在哪里找到它。