Crash in -[ASCollectionView forwardingTargetForSelector:] when deallocating a view controller while collectionView is updating
I've noticed this happening every now and then in my app, but just now got to investigating it further. The basic view controller setup is this:
- start off with no sections in my
ASCollectionView - in
-viewDidLoad, fetch data from a web service - when the data comes back, set up my data source, and do
-[ASCollectionView performBatchUpdates:completion:]to insert the new sections
I use -[ASCollectionView performBatchUpdates:completion:] so it has a nice animation when the data arrives. When simply inserting the new sections without an update block, there is no crash.
The crash occurs when you pop the viewController just after the collectionView updates. It seems that the viewController deallocates, but something related with the animations of the collectionView is causing -[ASCollectionView forwardingTargetForSelector:] to be called after everything is deallocated.
I've attached a patch file for the example project "ASCollectionView" that shows this happening. I basically mock a web service call that takes a couple seconds, and then pop the viewController at the same time. You can see everything working fine if you change the dispatch_after call that pops the viewController to some other time, like 1 second or 3 seconds.
A workaround I have in some places is to just use -[ASCollectionView reloadDataWithCompletion:] and handle the animations myself. This doesn't work everywhere though, as there's some places where I already have sections in the collectionView and need to insert new sections, rather than just reload the whole table.
@tomizimobile thanks for the report! What is the full backtrace?
@tomizimobile does this happen every time? I have the test project running, and haven't been able to make it occur in the simulator. Does it happen on device?
Note that your project was linking 1.9.3, and I am running against master / 1.9.4, so it is conceivable it is fixed in master - but I am far from confident of that at this point.
This PR touches related areas, and though not ready to land, could be modified to fix this issue being reported if I can reproduce it. I would like to fix this before launching 1.9.4 if you're able to get back to me with reproduction steps, otherwise I will launch the release tonight and fix this in master afterwards.
https://github.com/facebook/AsyncDisplayKit/pull/960
@appleguy Here's a backtrace: backtrace.txt
I just updated to master in case there was a fix, and it's still occurring for me every time. I'm testing on both an iPhone 6 and an iPhone 6+. I didn't run it in the simulator until just now, but it doesn't seem to happen in the simulator, probably due to the processor differences.
@tomizimobile ok, thanks!! I have also tried it on an iPod 5G and it still doesn't occur, but at least I'm using it as an optimization experiment (just pushed a commit with a few small optimizations lol). I'll try it on my 6S and I do have a 6+ with me at my family's house here, so will try that if needed.
Excellent, I was able to reproduce on the 6S. Thanks so much for making the test app - your reports and general assistance has been of great value to the project, @tomizimobile!
@tomizimobile this should be fully resolved in master. Please do test it and re-open this issue right away if you encounter any problems. Thanks a lot for your test app modifications - I actually integrated and landed them behind a #define for future testing / verification. In the next two months the community will be investing in a new setup for example apps, putting some apps like this one into more of a "testing / development" state rather than being in a directory that implies they are reference code.
@appleguy No problem, glad to help. Master seems to fix the test project, although it needs to have #import "AppDelegate.h" at the top of "ViewController.m" so it can see SIMULATE_WEB_RESPONSE.
I haven't been able to test this against my current project yet, as I realized the "CollectionRefactoring" branch made some changes that are causing my app to crash on a -[ASCollectionView reloadData] call. The backtrace wasn't helping me though - just gives me output like this:
* thread #1: tid = 0x1fea4c, 0x0000000125faaf40, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x125faaf40)
* frame #0: 0x0000000125faaf40
...
I don't really have time to look into it further right now, but when I get a chance I'll see what I can do.
@appleguy After updating to master this morning, everything seems ok for me in my current project.