7

I have a UICollectionView where I am filling up the cells with images downloaded from the Internet. For this I am using SDWebImage. My code looks like this:

-(UICollectionViewCell*) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *CellIdentifier = @"Gallery_Cell";

    GalleryCell *cell= (GalleryCell *)[self.flowCollection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    if (indexPath.row < self.collectionData.count) {

        CellDetails *dets = [self.collectionData objectAtIndex:indexPath.row];

        NSURL *mainImageURL = [NSURL URLWithString:dets.imageURL];

        cell.image.contentMode = UIViewContentModeScaleAspectFill;
        cell.image.clipsToBounds = YES;

        [cell.image setImageWithURL:mainImageURL placeholderImage:nil];
    }

    return cell;
}

I believe I have set this up correctly. But the app crashes (EXC_BAD_ACCESS) completely at random sometimes leaving this stack trace:

enter image description here

There is no other message in the log area. I tried setting an exception breakpoint, but each time this crash occurs, showing this stack trace. Does anyone have any idea what might be the problem?

Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85
  • exception breakpoint will not work here since this is a EXC_BAD_ACCESS. Means that it can be a segmentation fault / segmentation violation. Lot of chance to be a dangling pointer – Mr Bonjour Dec 06 '13 at 14:38
  • Ok. So how do I go about getting to the root of the problem? Perhaps zombie objects? – Rameez Hussain Dec 06 '13 at 14:40
  • I haven't worked with the collection view, but shouldn't you be checking if cell is nill after dequeueing it? And, if nill, to create the cell? – Jeremy Dec 06 '13 at 14:43
  • You should precise to us if the random crash always occur into the same stack crash (NSIndexPath indexAtPosition), or if it goes into other crash stack. That will help to understand the real problem – Mr Bonjour Dec 06 '13 at 14:44
  • 1
    But yeah, debug with zombie enabled. That should find and fix the ugly duck – Mr Bonjour Dec 06 '13 at 14:53
  • I enabled the zombies and tried it and it gave the same stack trace, but this time with an error saying: "[NSIndexPath section]: message sent to deallocated instance 0x1c1f8fb0" – Rameez Hussain Dec 06 '13 at 15:03
  • Did you find any solution to problem? – OzBoz Jul 18 '14 at 12:44
  • @OzBoz. Yes I did. I posted it as a separate question and found the solution myself. I have posted the answer here: http://stackoverflow.com/a/20465196/1082181. – Rameez Hussain Jul 18 '14 at 12:52
  • This does not seem to be an issue in iOS 8. Our crash analytics show many instances and 100% are iOS 7 devices. – Chris Feb 12 '15 at 13:42

3 Answers3

2

If anyone is looking for the answer, I have solved the problem and answered another one of my questions related to the same issue. You can find it here. Hope it helps!

Community
  • 1
  • 1
Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85
1

Did you register your CollectionViewCells?

If you did,

dequeueReusableCellWithReuseIdentifier:forIndexPath:

should create a cell for you if there is no cell to reuse.

In your

- (void)viewDidLoad

register your Cells with the CollectionView

UINib *galleryCellNib = [UINib nibWithNibName:@"GalleryCell" bundle:nil];
[self.collectionView registerNib:galleryCellNib forCellWithReuseIdentifier:@"Gallery_Cell"];

or

[self.collectionView registerClass:[GalleryCell class] forCellWithReuseIdentifier:@"Gallery_Cell"];

Depending if you're using xib's or just a class

muffe
  • 2,285
  • 1
  • 19
  • 23
0

This could also happen when one of the connections in InterfaceBuilder is no longer valid (when customizing a cell class - by inheriting from UITableViewCell or UICollectionViewCell). For instance: if the name of the control in the file has changed without updating the connection to it in InterfaceBuilder.

GK100
  • 3,664
  • 1
  • 26
  • 19