从后台返回时viewDidLoad崩溃

时间:2011-08-08 01:51:27

标签: objective-c ios memory-management autorelease 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中创建一个本地对象,将其添加到视图中,然后释放它,再也不用担心它了?好的,在ivars中使用alloc-init'ed视图,直到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。

感谢。

1 个答案:

答案 0 :(得分:0)

如果没有您的代码和产生的错误,这是我对可能的内存问题的最佳建议:首先,使用“产品”菜单中的“分析”功能,并解决所有引发的警告。如果您仍有问题,请对其进行分析并使用分配或泄漏分析器来跟踪您的记忆。最后,调试有问题的部分并查看内存堆栈,以查看与应用程序崩溃时相比,viewDidLoad中实际存在的内容。最近我偶然使用了assign而不是保留在我的一个属性中,最终遇到了类似的问题。如上所述,请确保将已释放的对象设置为nil。释放和删除保留属性的最快方法是:

self.myProperty = nil;