【发布时间】:2011-10-22 01:40:03
【问题描述】:
我最近启用了一个应用程序的多任务处理(在后台运行时不会退出),现在用户在不可预知的时间遇到崩溃。崩溃日志显示崩溃发生在 viewDidLoad 中。
无论如何,我的 viewDidLoad 方法可能需要重新考虑。我从 xib 加载了一些视图,然后以编程方式构建层次结构的其余部分(包括一些自动释放的 UIButton,我认为这是崩溃的罪魁祸首)。对可能发生的事情以及如何更好地解决这个问题有任何见解吗?
这是一个典型的崩溃报告:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x49ef527e
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x32da1c9a objc_msgSend + 18
1 App 0x0000a890 -[EntryViewController viewDidLoad] (EntryViewController.m:270)
2 UIKit 0x3241ff08 -[UIViewController view] + 104
3 UIKit 0x3242c1ce -[UIViewController viewControllerForRotation] + 34
4 UIKit 0x3242ca40 -[UIViewController shouldWindowUseOnePartInterfaceRotationAnimation:] + 8
5 UIKit 0x3242c9a8 -[UIWindow _clientsForRotation] + 236
6 UIKit 0x32494fea -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 70
7 UIKit 0x32493f9e -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 946
8 UIKit 0x324bec50 -[UIViewController _dismissModalViewControllerWithTransition:from:] + 1444
9 UIKit 0x324be5e0 -[UIViewController dismissModalViewControllerWithTransition:] + 596
10 UIKit 0x324be366 -[UIViewController dismissModalViewControllerAnimated:] + 86
最有帮助的是关于在加载/卸载周期中管理视图层次结构的最佳实践的简要说明。我知道 IBOutlet 应该保留在 viewDidUnload 中设置为 nil 的属性。
但是以编程方式创建的 UIView 和 UIControl 呢?确定在 viewDidLoad 中创建一个本地对象,将其添加到视图中,然后释放它,再也不用担心了吗?可以在 ivars 中使用分配初始化的视图,直到 dealloc 才发布?可以使用我从未发布的自动发布视图吗?
这是我的一些 viewDidLoad:
imagebg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
[self.view addSubview:imagebg];
[self positionBackgroundAt:0];
UIButton *flickbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[flickbtn addTarget:self action:@selector(flick:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:flickbtn];
[flickbtn release];
// SET UP UPPER LEFT BUTTONS
upperLeftButton0 = [UIButton buttonWithType:UIButtonTypeCustom];
[upperLeftButton0 setFrame:CGRectZero];
[self.view addSubview:upperLeftButton0];
upperLeftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[upperLeftButton1 setFrame:CGRectZero];
[self.view addSubview:upperLeftButton1];
// SET UP DISPLAY
displayimg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newdisplay.png"]];
[displayimg setCenter:CGPointMake(CGRectGetMidX(self.view.frame),CGRectGetMaxY(self.view.frame) - 57)];
[self.view addSubview:displayimg];
[displayimg release];
...
我的 viewDidUnload 只是将所有 IBOutlets 设置为 nil。
谢谢。
【问题讨论】:
-
你应该发布一些代码和你得到的错误堆栈。
-
正如 j0k 所说,您需要向我们展示一些代码来缩小问题范围
-
我想您访问的是在
viewDidUnload中发布的对象,该对象在发布后并未设置为nil。 -
显示您的
viewDidUnload和veiwDidLoad定义怎么样?
标签: objective-c ios memory-management autorelease viewdidload