2011-08-08 50 views
0

我最近启用了一个应用程序的多任务处理(不会在退出时退出),现在用户在不可预知的时间出现崩溃。崩溃日志显示崩溃发生在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只是将所有IBOutlets至零。

谢谢。

+0

你应该发布一些代码和你得到的错误堆栈。 – csano

+0

正如j0k所说,你需要向我们展示一些代码来缩小问题的范围 – REALFREE

+1

我想你访问在'viewDidUnload'中发布的对象,该对象在发布后没有设置为'nil'。 –

回答

0

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

self.myProperty = nil;