获取“EXC_BAD_ACCESS”当父视图控制器已被 viewDidUnload 后尝试解雇ModalViewController 时
我有一个名为“MainView”的视图控制器,它将调用
[self presentModalViewController:playView animated:NO];
插入一个“PlayView”视图控制器。
当应用程序在PlayView上运行时,如果收到applicationDidReceiveMemoryWarning消息,它将调用MainView的viewDidUnload函数并释放MainView对象。此时此刻,PlayView 还活着。一切都很好,直到用户单击按钮离开 PlayView,它(PlayView)将调用:
[self dismissModalViewControllerAnimated:NO];
然后应用程序因接收“EXC_BAD_ACCESS”错误消息而崩溃... 我认为原因是MainView对象消失了,当PlayView想要解散自己时,它找不到合适的ViewController来呈现。
如何解决这个问题? T_T
PS。 PlayView 视图控制器由 IB 创建,并在 MainView 中设置为保留属性。
I have a view controller named "MainView", it will call
[self presentModalViewController:playView animated:NO];
to insert a "PlayView" view controller.
When app is running at PlayView, if receiving an applicationDidReceiveMemoryWarning message, it will call MainView's viewDidUnload function and release the MainView object. In this moment, the PlayView is still alive. Every thing is fine until user click a button to leaving the PlayView, it(PlayView) will call:
[self dismissModalViewControllerAnimated:NO];
Then the application is crashed with receiving 'EXC_BAD_ACCESS' error message...
I think the reason is that MainView object is gone, when PlayView want to dismiss itself, it can't find a suitable ViewController to present.
How to fix this problem? T_T
PS. the PlayView view controller is created by IB, and it is set as a retain property in MainView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想问题出在你的MainView上。它有一些指向视图(位于主视图上)的出口或属性。内存警告 MainView.view 被卸载(因此它释放其子视图)后,如果您没有保留它们&还没有将它们设置为零,它们现在指向不存在的对象。所以你应该在 viewDidUnload 方法中将它们全部设置为 nil 。
I suppose the problem is with your MainView. It has some outlets or properties that point to views (that lay on the main view). After memory warning MainView.view is unloaded (so it releases its subviews) and if you haven't retained them & haven't set them to nil, they are now pointing to notexisting objects. So you should set them all to nil in viewDidUnload method.
我似乎找到了问题所在。
我在 MainView 下有一个名为“VCGameRule”的视图控制器。在 VCGameRule.h 文件中有如下声明:
在 VCGameRule.m 的初始化函数中有如下代码:
其 viewDidUnload 函数如下:
看到问题了吗?我尝试将新分配的 UILabel 对象分配给 lblInitialCash,这是保留的 IBOutlet 属性。我尝试释放 viewDidUnload 函数中的属性,然后发生了一些不好的事情......
我仍然不确定它到底发生了什么错误。但我想应该是该属性原来分配的内存变得混乱了。因此,当应用程序尝试在每个加载的视图控制器中调用 didReceiveMemoryWarning 时,我收到“EXC_BAD_ACCESS”错误消息。
当我将 IBOutlet 属性修改为普通的类变量后,该错误似乎不再发生!现在一切都很好,即使我的应用程序确实使用了大量内存并执行了多次 viewDidUnload 函数,应用程序仍然活着。 \(^o^)/
I seems find the problem.
I have a view controller named "VCGameRule", under the MainView. And it has a declaration in the VCGameRule.h file as follow:
In VCGameRule.m, there is a code as following in a initialization function:
And its viewDidUnload function is like this:
See the problem? I try to assign an new allocated UILabel object to lblInitialCash which is a retained IBOutlet property. And I try to release the property in viewDidUnload function, then some bad thing happens....
I am still not sure what the exactly error it occurs. But I think should be the property's original allocated memory become chaos. So I receive a "EXC_BAD_ACCESS" error message when the App try to call didReceiveMemoryWarning in every loaded view controllers.
After I modify the IBOutlet property to a normal class variables, the error seems not happen again! And everything is fine now, even my App really uses a lot of memory and do many times of viewDidUnload function, App is still alive. \(^o^)/