为什么释放视图控制器会导致崩溃?
我总是像这样将一个新的视图控制器推送到堆栈上:
MyViewController *vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
当将其从堆栈中弹出时,一切正常:
[self.navigationController popViewControllerAnimated:NO];
但是现在,当我将 vc 从堆栈中弹出时,我在 main.m 中崩溃,指出访问错误在行:int retVal = UIApplicationMain(argc, argv, nil, nil);
但现在如果我注释掉 [vc release] 就不会再崩溃了?
但是为什么这肯定会泄漏内存,因为我没有释放我创建的东西?
I always push a new view controller onto the stack like this:
MyViewController *vc = [[MyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
And all works well when it comes to popping it off the stack with:
[self.navigationController popViewControllerAnimated:NO];
But now when I pop the vc off the stack I get a crash in main.m stating a bad access at line:int retVal = UIApplicationMain(argc, argv, nil, nil);
But now if I comment out [vc release] no more crash?
But why and surely this leaks memory as Im not releasing something I created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的内存管理看起来不错。也许您对 vc 内部某些内容的内存管理不善。 MyViewController 的 dealloc 方法是什么样的?
我的猜测是您使用了不正确的 init 方法(可能是 initWithNibName:bundle:),并且您在 dealloc 中释放了从未正确初始化的 ivars。
Your memory management looks fine. Perhaps you are mismanaging the memory of something inside of your vc. What does the dealloc method of MyViewController look like?
My guess is you are using the incorrect init method (perhaps initWithNibName:bundle:) and you are releasing ivars in dealloc that were never properly initialized.
您是否尝试过将其用作
Did you try using it as a
导航控制器将保留
vc
,然后,当vc
弹出时,navigationController
释放它并vc
释放。所以,你必须留下发布代码,它是正确的。
我认为你必须使用
initWithNibName:bundle:
代替init
。Navigation controller will retain
vc
then, whenvc
is popped,navigationController
releases it andvc
deallocs.So, you must leave the release code, it is correct.
I think you have to use a
initWithNibName:bundle:
insted of theinit
.它们不同的原因是您没有分配文本对象,因此您不是所有者。分配和释放它们是 IB 的工作,它也确实做到了。
所以如果你也尝试释放它,就会出现问题。
The reason why they are different is that you are not allocating the text objects, and therefore you are not the owner. It is the IB's job to alloc and realese them, which it does.
So if you also try to release it, it will cause problems.
@brandontreb 的这句话真的对我很有帮助!在“收到模拟内存警告”之后,我花了一整天的时间来修复崩溃,具体描述如下:
防止 uinavigationcontroller 设置中 popViewControllerAnimated 的错误访问崩溃
在我推送的视图控制器的 loadView: 中,将视图控制器自身传递给其数据源的 init :.
而 dataSource 类保留它,如:
修复崩溃只需更改为:
并删除:
宾果!我还是不知道为什么!作为viewController在dataSource的dealloc:中释放。
This sentence from @brandontreb really helped me! I've struggled a whole day to fix the crash after a 'Received simulated memory warning', exactly descripted like:
Preventing bad access crash for popViewControllerAnimated in uinavigationcontroller setup
In my pushed view controller's loadView:, passed the view controller self to its dataSource's init:.
while the dataSource class retained it like:
Fix the crash just change to:
and remove:
bingo! I still don't know why! As viewController released in dealloc: of dataSource.