每次按下按钮时,我都会遇到一个奇怪的问题:
- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
MyObject* obj = ...
MyViewController* detailView = [[MyViewController alloc] init]; // line A
[self.navigationController pushViewController:detailView animated:YES];
[detailView setObject:obj];
detailView = nil; // should I also release it? -- line B
}
问题是我道歉我必须发布detailView(内存工具显示我有内存泄漏是不是没有完成),也因为navigationController应该保留我的detailView,但是如果我尝试在行中添加autorelease“一个“或在行”B“,或者只是在行”B“中的detailView的发布(当然在分配它之前为nil),程序崩溃并发送一个EXC_BAD_ACCESS'原因释放消息发送给解除分配的实例[CALayer] ... < / p>
有什么想法吗? 非常感谢
答案 0 :(得分:2)
以这种方式试试
- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
MyObject* obj = ...
MyViewController* detailView = [[MyViewController alloc] init];
[detailView setObject:obj];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
detailView = nil; // now this will be optional
}
答案 1 :(得分:1)
这可以不崩溃吗?
- (void)launcherView:(TTLauncherView*)lnchr didSelectItem:(TTLauncherItem*)itm {
MyObject* obj = ...
MyViewController* detailView = [[MyViewController alloc] init];
[self.navigationController pushViewController:detailView animated:YES];
//[detailView setObject:obj]; // <- What's this for?
[detailView release]
}
答案 2 :(得分:0)
尝试“initwithnibname”
无关但如果追逐内存泄漏,请不要忘记发布 MyObject
答案 3 :(得分:-1)
当你设置detailView = nil;
而不释放它时,你只能指向内存的指针。在您释放内存块之前,仍会分配内存块。
您必须在[detailView release]
之前使用detailView = nil
,否则您将无法再次引用该内存块(内存泄漏)。