AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

[ASTableNode] Deallocation while inside of a collection view may cause UIKit error.

Open tomizimobile opened this issue 11 years ago • 8 comments

My setup: I have an ASCollectionView which contains an ASCellNode, which contains an ASTableNode. To set up the ASTableNode, i assign the asyncDataSource and asyncDelegate in -didLoad of the containing cell, since that's when I know the backing ASTableView is ready. Then I call setNeedsLayout() on the cell so it can lay out properly. The collection view is in a modally-presented view controller. The UI loads and lays out just fine - it's rough around the edges, but it's similar to the layout I'm using in my current project.

The issue is I get a crash in -[ASCollectionView dealloc] when dismissing the view controller. I've noticed that if I don't call setNeedsLayout() in -didLoad, the crash doesn't occur, but the cell isn't laid out properly. Is there a better way to set the ASTableNode asyncDataSource and asyncDelegate, or some other layout spec solution that gives me the layout I'm looking for?

I've created a test project that demonstrates this: https://github.com/tomizimobile/ASDK_CollectionCellNode-TableNode-crash. It occurs using the latest release, 1.9.2.

tomizimobile avatar Nov 30 '15 17:11 tomizimobile

@tomizimobile thanks for the test project! This is unusual, not something I've seen before even though I have been focusing on this very use case (Collections containing Tables and other Collections; Tables containing Collections).

I am now planning a 1.9.3 to launch by the end of this weekend to resolve two recently discovered issues. I will make this the third, and expect to have a solution for you in the next couple days. Let me know if you discover any additional issues, or a fix / workaround for this one.

appleguy avatar Dec 04 '15 03:12 appleguy

@nguyenhuy, @levi this one is a bit unusual. Did something leave an update transaction open? I am still investigating, but definitely surprised.

2015-12-03 19:48:16.933 ASDK_CollectionCellNode-TableNode-crash[53944:10304775] *** Assertion failure in -[ASCollectionView dealloc], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UICollectionView.m:737
2015-12-03 19:48:16.945 ASDK_CollectionCellNode-TableNode-crash[53944:10304775] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView (<ASCollectionView: 0x7fc43d034e00; baseClass = UICollectionView; frame = (0 0; 375 667); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fc43be18940>; layer = <CALayer: 0x7fc43be2a070>; contentOffset: {0, -64}; contentSize: {375, 63}> collection view layout: <UICollectionViewFlowLayout: 0x7fc43bd2dc40>) was deallocated while an update was in flight'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000100b19f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010283ddeb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000100b19daa +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x0000000100f625ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   UIKit                               0x0000000101b7acf5 -[UICollectionView dealloc] + 608
    5   AsyncDisplayKit                     0x00000001007c5dcb -[ASCollectionView dealloc] + 139
    6   libobjc.A.dylib                     0x00000001028520b8 _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 488
    7   CoreFoundation                      0x0000000100a07916 _CFAutoreleasePoolPop + 22
    8   CoreFoundation                      0x0000000100a3b8b1 __CFRunLoopRun + 2081
    9   CoreFoundation                      0x0000000100a3ae08 CFRunLoopRunSpecific + 488
    10  GraphicsServices                    0x00000001060b1ad2 GSEventRunModal + 161
    11  UIKit                               0x000000010133630d UIApplicationMain + 171
    12  ASDK_CollectionCellNode-TableNode-crash 0x000000010070969d main + 109
    13  libdyld.dylib                       0x0000000103c0892d start + 1

appleguy avatar Dec 04 '15 03:12 appleguy

This is the only reference that Google returned for that exception message - wow! It does have some relevant information.

https://github.com/Citrrus/MBContactPicker/issues/66

appleguy avatar Dec 04 '15 03:12 appleguy

@tomizimobile so I believe this is fixed in master - please try it out and let me know what you find! I also verified memory behavior — using Leaks / Allocations tool, ensured all of the ASDK-associated objects that are created by the button press are torn down. It seems stable over multiple launches of the modal view.

I was studying the code for this and realized the most likely cause is a "performOnMain" call that happens just before deallocation. Then the main call occurs with something like reloadData, in the next runloop, and triggers the layout assertion.

Then I realized that a diff I had open to review would almost certainly address this condition. Turns out it did, which is an awesome coincidence (and shows how a layout change triggered by tear-down could even cause asynchronous data reloading -- no good!). https://github.com/facebook/AsyncDisplayKit/pull/898 Thank you @Adlai-Holler !

appleguy avatar Dec 04 '15 05:12 appleguy

Haha awesome! My pleasure!

Adlai-Holler avatar Dec 04 '15 05:12 Adlai-Holler

@appleguy Fantastic! Master fixes that issue in my current project, as well as another workaround I had in place with some other ASCellNode objects that contain an ASTableNode that I just ran into yesterday.

tomizimobile avatar Dec 04 '15 13:12 tomizimobile

@tomizimobile great news! Bummed you then promptly ran into the deadlock - lol :(. We'll get this patched up quickly.

appleguy avatar Dec 06 '15 00:12 appleguy

@tomizimobile I'm the author of this bug and would like to express my sincere apology! :cry:

nguyenhuy avatar Dec 09 '15 18:12 nguyenhuy