I’m getting a “Potential leak” message upon doing an Analyze run of this code – which works perfectly well by the way, with no errors or crashes (its just a simple UINavigationController/TableView bit.)
The full message I get is: “Potential leak of an object allocated and stored into ‘tempKey'”
It doesn’t make sense to me – can anyone see it?
- (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];
}
The analyzer is correct. If you do this:
You have a pointer to an NSString that you own. If you then do this:
You have assigned
someStringto point to a new NSString object, and leaked the first. That line does not simply change the existing string’s contents. This is exactly what you’re doing with yourtempKey.