我的didSelectRowAtIndexPath函数导致内存泄漏

时间:2012-08-23 10:54:00

标签: iphone objective-c ios memory-leaks

我在这里的第一篇文章。好像我遇到了一些内存泄漏问题。

我有一个带有两列的标签栏,一个用于沙拉,一个用于寿司。在每个标签中,我还有一个显示不同食物选择的tableview。当用户按下其中一个单元格时,应用程序会将用户带到更详细的视图,该视图会显示更大的图像以及有关所选食物菜单的一些信息。

但是当我运行泄漏时,我在下一行得到了98.5%的泄漏。 [self.navigationController pushViewController:detail animated:YES]; 如果我在此行之后发布详细信息,则应用程序崩溃。

以下是功能代码的其余部分:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *message = nil;
NSMutableString *image_string = nil;

DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detailview"];

if(tableView == SushiTable)
{
    message = [sushiNames objectAtIndex:indexPath.row];
    image_string = [NSMutableString stringWithString:[sushiImages objectAtIndex:indexPath.row]];
}
if(tableView == SaladTable)
{
    message = [saladNames objectAtIndex:indexPath.row];
    image_string = [NSMutableString stringWithString:[saladImages objectAtIndex:indexPath.row]];
}

[image_string deleteCharactersInRange: [image_string rangeOfString: @"_small"]];

NSMutableString *temp_str = [[message copy] autorelease];
NSString *final = [[[temp_str stringByReplacingOccurrencesOfString:@" " withString:@"_"] 
                   stringByReplacingOccurrencesOfString:@"ä" withString:@"a"] 
                   lowercaseString];

detail.food_name = message;
detail.image_name = image_string;
detail.food_info_key = final;

[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.navigationController pushViewController: detail animated: YES];
detail = nil;

}

编辑: 我在DetailViewController中的viewDidLoad函数中也看到了以下行有内存泄漏,不知道它是否相关。

food_image.image = [UIImage imageNamed:image_name];

EDIT2: 我也在模拟器上运行。

2 个答案:

答案 0 :(得分:3)

试试吧。

 [self.navigationController pushViewController: detail animated: YES];
 [detail release];
 detail = nil;

当你按下一个对象时。它现在被添加到导航堆栈数组中,现在是该对象的数组所有者 所以你应该需要在数组中添加的释放对象。

答案 1 :(得分:1)

全局声明detail并在dealloc:中发布。它可能有用。