由于推送和弹出 UIViewControllers,UINavigationController 崩溃
问题: 我有一个 UINavigationController 作为 UIWindow 的子视图、一个 rootViewController 类和一个自定义 MyViewController 类。以下步骤将获得 Exc_Bad_Access,100% 可重现。:
[myNaviationController pushViewController:myViewController_1stInstance animated:YES];
[myNaviationController pushViewController:myViewController_2ndInstance animated:YES];
点击左侧背面的 TapBarItem 两次(弹出两个 myViewController 实例)以显示 rootViewController。
经过痛苦的 1/2 天的尝试和错误,我终于找到了答案,但也提出了一个问题。
解决方案:我在 .m 文件中声明了许多对象,作为声明私有变量的一种惰性方式,以避免弄乱 .h 文件。例如,
#impoart "MyViewController.h"
NSMutableString*variable1;
@implement ...
-(id)init
{
...
varialbe1=[[NSMutableString alloc] init];
...
}
-(void)dealloc
{
[variable1 release];
}
由于某些原因,当加载 myViewController_2ndInstance 的视图后卸载 myViewController_1stInstance 的视图(但仍在导航控制器的堆栈中)时,iPhone 操作系统可能会丢失对这些“惰性私有”变量内存分配的跟踪。第一次点击后面的 TapBarItem 是可以的,因为 myViewController_2ndInstance'view 仍然加载。但是背面 TapBarItem 上的第二次点击给我带来了麻烦,因为它试图释放第一个实例。它调用[变量释放]导致Exc_Bad_Access,因为它随机指向(松散的指针)。
要解决此问题很简单,只需在 .h 文件中将变量 1 声明为 @private 即可。
这是我的问题: 我已经使用“惰性私有”变量相当长一段时间了,没有任何问题,直到它们涉及 UINavigationController 为止。这是 iPhone 操作系统中的错误吗?或者我对 Objective C 存在根本性的误解?
The issue:
I have a UINavigationController as as subview of UIWindow, a rootViewController class and a custom MyViewController class. The following steps will get a Exc_Bad_Access, 100% reproducible.:
[myNaviationController pushViewController:myViewController_1stInstance animated:YES];
[myNaviationController pushViewController:myViewController_2ndInstance animated:YES];
Hit the left back tapBarItem twice (pop out two of the myViewController instances) to show the rootViewController.
After a painful 1/2 day of try and error, I finally figure out the answer but also raise a question.
The Solution: I declared many objects in the .m file as a lazy way of declaring private variables to avoid cluttering the .h file. For instance,
#impoart "MyViewController.h"
NSMutableString*variable1;
@implement ...
-(id)init
{
...
varialbe1=[[NSMutableString alloc] init];
...
}
-(void)dealloc
{
[variable1 release];
}
For some reasons, the iphone OS may loose track of these "lazy private" variables memory allocation when myViewController_1stInstance's view is unloaded (but still in the navigation controller's stacks) after loading the view of myViewController_2ndInstance. The first time to tap the back tapBarItem is ok since myViewController_2ndInstance'view is still loaded. But the 2nd tap on the back tapBarItem gave me hell because it tried to dealloc the 1st instance. It called [variable release] resulted in Exc_Bad_Access because it pointed randomly (loose pointer).
To fix this problem is simple, declare variable1 as a @private in the .h file.
Here is my Question:
I have been using the "lazy private" variables for quite some time without any issues until they are involved in UINavigationController. Is this a bug in iPhone OS? Or there is a fundamental misunderstanding on my part about Objective C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能与使用相同静态分配变量的视图控制器的两个实例相关。
换句话说,
myViewController_1stInstance
和myViewController_2ndInstance
都使用内存中相同的variable1
位置并相互覆盖。在
@interface
定义之后在大括号内声明的变量具有运行时为该类的每个实例分配的内存位置(每次调用[alloc]
。在 Objective-C 中,但您可以在编译时对其他实例隐藏它们,如所述 此处。
It might be related to both instances of your view controller using the same statically-allocated variable.
In other words, both
myViewController_1stInstance
andmyViewController_2ndInstance
are using the samevariable1
location in memory and overwriting one another.Variables declared inside of the curly braces after your
@interface
definition have a memory location allocated by the runtime for each instance of the class (every time you call[<ClassName> alloc]
. Variables declared in the global scope (that is, outside of any functions or class declarations) are just that: global. That means that the variable can only hold one value per running copy of your application.There are no truly private variables in Objective-C, but you can hide them from other instances at compile time as described here.
反应有点晚,但我以前见过这个问题。不要同时推送两个
viewControllers
动画。推第一个没有动画,推第二个有动画。UINavigationController
无法同时处理两个动画。A bit of a late reaction, but I've seen this problem before. Don't push two
viewControllers
animated at the same time. Push the first one without animation and push the second one with animation.UINavigationController
can't handle two animations at the same time.