UITableView中的崩溃发送消息到解除分配的UIViewController

Kam*_*tka 4 crash core-animation objective-c uitableview ios

我一直在与下面的堆栈跟踪作斗争.我无法重现这个问题,但我知道它发生在很多用户身上(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
Run Code Online (Sandbox Code Playgroud)

...

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

看一下堆栈跟踪,我猜测在一些动画或慢速操作进行时,视图控制器将被解除分配.在尝试了所有内容之后,我无法找到发生这种情况的配置.

您是否有任何关于为什么在其视图仍然是动画时可以取消分配VC的建议?

Kam*_*tka 8

我注意到这次崩溃恰好与切换UITableViewControllerUIViewController+ UITableView.

据推测,在UITableViewController取消分配时,表格视图的委托和数据源很小.新代码并非如此.

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

- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
}
Run Code Online (Sandbox Code Playgroud)

他们需要设置为nil的原因是UITableView的dataSource和delegate都是unsafe_unretained引用.