【问题标题】:viewDidLoad crashes when returning from background从后台返回时 viewDidLoad 崩溃
【发布时间】: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
  • 显示您的viewDidUnloadveiwDidLoad 定义怎么样?

标签: objective-c ios memory-management autorelease viewdidload


【解决方案1】:

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

self.myProperty = nil;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 2021-10-30
    • 2012-01-16
    • 2013-07-10
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多