Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView crash on unhighlightAllItems

I've gotten several crash reports related to a UICollectionView in iOS 7. I'm not able to consistently recreate this crash.

Exception Type:  SIGSEGV
Exception Codes: SEGV_ACCERR at 0x91c4392b
Crashed Thread:  0

Application Specific Information:
*** Terminating app due to uncaught exception '', reason: ''

Thread 0 Crashed:
0   libobjc.A.dylib                     0x39dd2b26 objc_msgSend + 6
1   UIKit                               0x31fd5eef -[UICollectionView cellForItemAtIndexPath:] + 111
2   UIKit                               0x32060bfd -[UICollectionView _unhighlightItemAtIndexPath:animated:notifyDelegate:] + 149
3   UIKit                               0x32383947 -[UICollectionView _unhighlightAllItems] + 151
4   UIKit                               0x3205f9fb -[UICollectionView touchesBegan:withEvent:] + 367
5   UIKit                               0x31fcb101 forwardTouchMethod + 233
6   UIKit                               0x31fcb101 forwardTouchMethod + 233
7   UIKit                               0x31e3be4b _UIGestureRecognizerUpdate + 5523
8   UIKit                               0x31e73c41 -[UIWindow _sendGesturesForEvent:] + 773
9   UIKit                               0x31e735e7 -[UIWindow sendEvent:] + 667
10  UIKit                               0x31e48a25 -[UIApplication sendEvent:] + 197
11  UIKit                               0x31e47221 _UIApplicationHandleEventQueue + 7097
12  CoreFoundation                      0x2f69e18b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13  CoreFoundation                      0x2f69d6e1 __CFRunLoopDoSources0 + 341
14  CoreFoundation                      0x2f69be4f __CFRunLoopRun + 623
15  CoreFoundation                      0x2f606ce7 CFRunLoopRunSpecific + 523
16  CoreFoundation                      0x2f606acb CFRunLoopRunInMode + 107
17  GraphicsServices                    0x342f4283 GSEventRunModal + 139
18  UIKit                               0x31ea8a41 UIApplicationMain + 1137
19  JackThreadsIpad                     0x000922b7 main (main.m:16)

The UICollectionViewCells in the app share a common superclass that manages highlighting. When the cell is highlighted the alpha changes.

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];

    if (highlighted) {
        self.alpha = 0.8;
    } else {
        self.alpha = 1.0;
    }
}

Could calling [super setHighlighted:highlighted] cause a crash like this? The app was compiled and submitted with XCode 4 and is only happening on iOS 7. Any other suggestions to figure out where this is happening. Thanks for your help.

Edit: I was able to catch this in the debugger, but it still is not consistently reproducible. The crash is:

[NSIndexPath section] message sent to deallocated instance XXXXXXXX
like image 413
Scott Bossak Avatar asked Sep 26 '13 15:09

Scott Bossak


3 Answers

If you are calling reloadData while the user is dragging the view, that might be the reason.

I had crashes related to this with similar crash reports and "fixed" the issue by delaying the reloadData call until after the user has finished scrolling the view. E.g. create a wrapped method instead of calling reloadData directly.

- (void)updateData {
     if (self.collectionView.isTracking) {
         self.updateDataOnScrollingEnded = YES;
     } else {
         [self.collectionView reloadData];
     }
}

Then when scrolling ends, call the updateData method (if needed) from the scroll view's delegate methods.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self scrollViewStopped:scrollView];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewStopped:scrollView];
}

- (void)scrollViewStopped:(UIScrollView *)scrollView
{
    if (self.updateDataOnScrollingEnded) {
         [self updateData];
         self.updateDataOnScrollingEnded = NO;
     }
}

My guess is that there is a weak reference to the highlighted cell's indexPath somewhere inside of the collectionView, and that calling reload will dealloc that indexPath. When the collectionView then tries to unhighlight the cell, it crashes.

EDIT:

As mentioned in comments below, this "solution" has some flaws. While investigating the issue further, it seems that in my case the problem had to do with multiple reloadData calls being queued on the main thread during the dragging of the collection view. When there was only one reloadData call, everything was fine, but whenever there was more than one – crash!

Since I always had exactly one section in my collectionView i replaced the reloadData call with

reloadSections:[NSIndexSet indexSetWithIndex:0]

However, this causes the cells to quickly fade out and back in again which I avoided with the following method (it would probably be better off as a category on the collection view)

- (void)reloadCollectionView:(UICollectionView *)collectionView animated:(BOOL)animated
{
    [UIView setAnimationsEnabled:animated];
    [collectionView performBatchUpdates:^{
        [collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    } completion:^(BOOL finished) {
        [UIView setAnimationsEnabled:YES];
    }];
}

So far, this has worked well for me and it also allows for the data to actually be updated while scrolling.

like image 125
toostn Avatar answered Nov 17 '22 09:11

toostn


Not sure after identifying only this piece of code. But as crash signal (SIGSEGV) seems due to memory leak. You just go to your Xcode setting and inside Edit Scheme jsut enable Zombie option and then try to reproduce your crash. It will show you the controller class name of method or any crash related information inside console of Xcode. And also just try to modify you condition below:-

- (void)setHighlighted:(BOOL)highlighted {

     //just comment this line or write this line to the below and check
     //[super setHighlighted:highlighted];
    if (highlighted) {
        self.alpha = 0.8;
    } else {
        self.alpha = 1.0;
    }
    [super setHighlighted:highlighted];
}
like image 43
Hussain Shabbir Avatar answered Nov 17 '22 10:11

Hussain Shabbir


I had this problem, though slightly different crash. Fixed by holding off any reloadData until the highlight is cleared. While toostn's suggestion would fix the issue, it is useful to be able to reloadData whilst scrolling, but doesn't make much sense when highlighting - as you have your finger on a cell.

implement the following UICollectionViewDelegate methods:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    self.allowReload = NO;
    return YES;
}


- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    self.allowReload = YES;
    [self reloadIfNecessary]; // calls reloadData if it is necessary to do so!
}
like image 22
Nick Hingston Avatar answered Nov 17 '22 09:11

Nick Hingston