viewDidLoad从后台返回时崩溃



我最近在一个应用程序中启用了多任务处理(在后台运行时不会退出),现在用户会在不可预测的时间崩溃。崩溃日志显示崩溃发生在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中创建一个本地对象,把它添加到视图中,然后释放它,再也不用担心了?在变量中使用alloc-init视图直到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只是将所有iboutlet设置为nil。

谢谢。

如果没有您的代码和导致的错误,这里是我对可能的内存问题的最佳建议:首先,使用"Product"菜单中的"Analyze"功能,并解决所提出的所有警告。如果仍然存在这个问题,请对其进行分析,并使用分配或泄漏分析器来跟踪内存。最后,调试有问题的部分,并查看内存堆栈,以查看viewDidLoad中实际存在的内容,并与应用程序崩溃时进行比较。最近,我不小心在我的一个属性中使用了分配而不是保留,并最终出现了类似的问题。如上所述,确保将释放的对象设置为nil。释放和清除保留属性的最快方法是:

self.myProperty = nil;

最新更新