我是Objective C的新手并且相信我在此函数中有内存泄漏情况,但我不确定何时删除/释放对象。
由于我将recipeObject
存储到我的视图中,因此我在视图的dealloc
中将其释放,但我不确定该视图?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RecipeObject * recipeObject = (RecipeObject *)[maRecipes objectAtIndex:indexPath.row];
RecipeView * recipeView = [[RecipeView alloc] initWithNibName:@"RecipeView" bundle:nil];
[recipeView setRecipeObject:RecipeObject];
[self.navigationController pushViewController:recipeView animated:YES];
}
有人能告诉我一个做什么或解释的例子吗?
答案 0 :(得分:4)
要记住在Objective-C中处理内存管理的第一条规则是,您要负责(1)分配(使用alloc
),(2)new up(使用new
),(3)复制(使用copy
),或(4)保留(使用retain
)。在这四种情况下,您必须明确release
(或autorelease
)这些参考。
在您的示例中,由于您已分配recipeView,因此必须在将其添加到导航控制器后将其释放。
RecipeView * recipeView = [[RecipeView alloc] initWithNibName:@"RecipeView" bundle:nil];
[self.navigationController pushViewController:recipeView animated:YES];
[recipeView release];
如果你不这样做,你就会泄漏recipeView,因为一旦方法退出,它就会超出范围,你将无法再访问堆上的已分配空间。
这有意义吗?
答案 1 :(得分:0)
您必须在recipeView
之后发布pushViewController
。
当按下它时,保留完成,然后弹出释放时。
答案 2 :(得分:0)
将其添加到代码的最后一行
[recipeView release];