在对此代码执行分析运行时,我收到了“潜在泄漏”消息 - 顺便说一句,它完美地工作,没有错误或崩溃。只是一个简单的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];
}
嗯,我试着简单地声明'tempKey'没有'分配它。 (含义:'NSString * tempKey;' - 没有'alloc'或'init'。)但是之后几行,在'artStaffVC.key = tempKey;'我得到这个消息:“分配的值是垃圾或未定义的”。所以我想这不是正确的做法。所以....我该如何解决这个问题?什么是正确的方法来做到这一点? – sirab333
@ sirab333,最好将指针初始化为零,而不是简单地声明它。如果试图在不分配它的情况下使用该字符串,这在理论上会阻止“EXC_BAD_ACCESS”。所以试试'NSString * tempKey = nil;'而不是'NSString * tempKey = [[NSString alloc] init];'。 –
工作! :-)现在只需弄清楚为什么和如何:-)感谢您的帮助。 – sirab333