【问题标题】:Crash in UITableView sending message to deallocated UIViewControllerUITableView 向释放的 UIViewController 发送消息时崩溃
【发布时间】:2014-10-01 17:48:25
【问题描述】:

我整天都在与以下堆栈跟踪作斗争。我无法重现该问题,但我知道很多用户都会遇到此问题 (Crashlytics)。

Crashed: com.apple.main-thread
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x0000000000000010

Thread : Crashed: com.apple.main-thread
0  libobjc.A.dylib                0x0000000194187bd0 objc_msgSend + 16
1  UIKit                          0x000000018860639c -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 544
2  UIKit                          0x00000001885fafc4 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2360
3  UIKit                          0x00000001883f0c60 -[UITableView layoutSubviews] + 172
4  UIKit                          0x000000018830d874 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 572
5  QuartzCore                     0x0000000187c65d58 -[CALayer layoutSublayers] + 168
6  QuartzCore                     0x0000000187c60944 CA::Layer::layout_if_needed(CA::Transaction*) + 320
7  QuartzCore                     0x0000000187c607e8 CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
8  QuartzCore                     0x0000000187c5ffe8 CA::Context::commit_transaction(CA::Transaction*) + 276
9  QuartzCore                     0x0000000187c5fd6c CA::Transaction::commit() + 436
10 UIKit                          0x0000000188304848 _afterCACommitHandler + 156
11 CoreFoundation                 0x0000000183b46388 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
12 CoreFoundation                 0x0000000183b43314 __CFRunLoopDoObservers + 360
13 CoreFoundation                 0x0000000183b436f4 __CFRunLoopRun + 836

...

这发生在拥有 UITableView 的 UIViewController 中。视图控制器也是表视图的数据源和委托。

查看堆栈跟踪,我猜测视图控制器将在某些动画或慢速操作进行时被释放。在尝试了一切之后,我找不到会发生这种情况的配置。

您对为什么 VC 可以在其视图之一仍在动画时被释放有什么建议吗?

【问题讨论】:

  • 您能否通过在 UIViewController 的 dealloc 方法中添加 NSLog 语句来验证您的理论是否正确?
  • 我可以,但正如我所提到的,我无法重现该问题,我需要为此完成发布新更新的整个周期。理想情况下,我会在此之前修复它:)
  • 我实际上只是注意到这次崩溃恰逢从 UITableViewController 切换到 UIViewController + UITableView。据推测,UITableViewController 正在取消委托和数据源。我认为取消他们将解决问题。但是,你知道当控制器被销毁时,什么时候可以调用委托吗?

标签: ios objective-c uitableview crash core-animation


【解决方案1】:

我注意到这次崩溃恰逢从UITableViewController 切换到UIViewController + UITableView

据推测,UITableViewController 在释放表视图的委托和数据源时正在取消它。新代码并非如此。

为了解决这个问题,我在- (void)dealloc中将它们设置为nil:

- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}

之所以需要将它们设置为nil,是因为both the UITableView's dataSource and delegate are unsafe_unretained references

【讨论】:

  • 谢谢,这很有帮助。我想知道我怎么会错过关于分配的部分,即 UITableView 的 .delegate 和 .dataSource 属性的 unsafe_unretained 所有权声明。我一直认为这是一种弱的声明。
  • 非常感谢您分享您的解决方案,这个问题困扰了我很长时间!
  • 事实证明这是相关的:stackoverflow.com/questions/9963677/…
猜你喜欢
  • 2012-05-20
  • 1970-01-01
  • 2013-04-13
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 2011-08-19
  • 2015-10-02
相关资源
最近更新 更多