[UICollectionView layoutAttributesForItemAtIndexPath:] crash sometimes below iOS11

hi,

I got an crash in my App(only iOS11 and below), accidental appearance. Has no clue why this comes from, and how to avoid?


In my viewcontroller BBANovelWiseToNAReaderVC , implemention of UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

.....

[self.collectionView layoutAttributesForItemAtIndexPath:bottomIndexPath]; // here crash!

....

}


Crash stacks:

Thread 0 Crashed: Queue: com.apple.main-thread 0

UIKit -[UICollectionViewData layoutAttributesForItemAtIndexPath:] (in UIKit) + 244 1

UIKit -[UICollectionView layoutAttributesForItemAtIndexPath:] (in UIKit) + 60 2

BaiduBoxApp -[BBANovelWiseToNAReaderVC scrollViewDidScroll:] (in BaiduBoxApp) (BBANovelWiseToNAReaderVC.m:0) 3

UIKit -[UIScrollView(UIScrollViewInternal) _notifyDidScroll] (in UIKit) + 76 4

UIKit -[UIScrollView setContentOffset:] (in UIKit) + 460 5

UIKit -[UIScrollViewScrollAnimation setProgress:] (in UIKit) + 308 6

UIKit -[UIAnimator(Static) _advanceAnimationsOfType:withTimestamp:] (in UIKit) + 280 7

QuartzCore CA::Display::DisplayLinkItem::dispatch() (in QuartzCore) + 40 8

QuartzCore CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) (in QuartzCore) + 424 9 IOKit IODispatchCalloutFromCFMessage (in IOKit) + 372 10

CoreFoundation __CFMachPortPerform (in CoreFoundation) + 180 11

CoreFoundation _CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ (in CoreFoundation) + 56 12 CoreFoundation __CFRunLoopDoSource1 (in CoreFoundation) + 436 13

CoreFoundation __CFRunLoopRun (in CoreFoundation) + 1800 14

CoreFoundation CFRunLoopRunSpecific (in CoreFoundation) + 384 15

GraphicsServices GSEventRunModal (in GraphicsServices) + 180 16

UIKit UIApplicationMain (in UIKit) + 204 17

BaiduBoxApp main (in BaiduBoxApp) (main.m:24) 18 ??? 0x00000001816f28b8 0x0 + 0

Replies

In addition, bottomIndexPath already has bounds-checked. Not seems like out of index problem.

How about change

[self.collectionView layoutAttributesForItemAtIndexPath:bottomIndexPath];

to
[self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:bottomIndexPath];


also

- (void)dealloc {

self.collectionView.delegate = nil;

}


will this works ?

You ask if this will work. Can't you test it, only you have the full code to provide the context for testing.


But effectively, layoutAttributesForItemAtIndexPath

is a property of collectionViewLayout, not of collectionView