Crash when scrolling to bottom #40

Closed
dxclancy opened this Issue Sep 20, 2011 · 2 comments

Projects

None yet

3 participants

@dxclancy

Launch on iOS 5 simulator, scroll to bottom, EXEC_BAD_ACCESS

  • (void)_cleanupCells
    {
    const CGRect visibleBounds = { self.contentOffset, self.bounds.size };

    [_visibleCells enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { <<<<<<<<<EXEC_BAD_ACCESS
    KKGridViewCell *cell = (KKGridViewCell *)obj;
    KKIndexPath *indexPath = (KKIndexPath *)key;
    if (!KKCGRectIntersectsRectVertically(cell.frame, visibleBounds)) {
    [cell removeFromSuperview];
    [_visibleCells removeObjectForKey:indexPath];
    [self _enqueueCell:cell withIdentifier:cell.reuseIdentifier];
    }

    }];
    }
    #0 0x0140009f in objc_msgSend ()
    #1 0x00020000 in pvars ()
    #2 0x00007943 in __27-[KKGridView _cleanupCells]_block_invoke_0 at /Users/dxclancy/Downloads/kolinkrewinkel-KKGridView-88e2e5d/KKGridView/KKGridView.mm:471
    #3 0x01256398 in ____NSDictionaryEnumerate_block_invoke_4 ()
    #4 0x011715e5 in CFBasicHashApply ()
    #5 0x011ee51a in __NSDictionaryEnumerate ()
    #6 0x011ee065 in -NSDictionary enumerateKeysAndObjectsUsingBlock:
    #7 0x000078c4 in -KKGridView _cleanupCells
    #8 0x00006c11 in -KKGridView _layoutVisibleCells
    #9 0x00004ce0 in -KKGridView _layoutGridView
    #10 0x00004c77 in -KKGridView layoutSubviews
    #11 0x002e9101 in -UIView(CALayerDelegate) layoutSublayersOfLayer:
    #12 0x001bf0d7 in -CALayer layoutSublayers
    #13 0x001c9a70 in CA::Layer::layout_if_needed(CA::Transaction*) ()
    #14 0x00151cd7 in CA::Context::commit_transaction(CA::Transaction*) ()
    #15 0x00153512 in CA::Transaction::commit() ()
    #16 0x000f8470 in CA::Transaction::flush_transaction() ()
    #17 0x001e8747 in CA::Display::DisplayLink::dispatch(unsigned long long, unsigned long long) ()
    #18 0x001e9763 in CA::Display::EmulatorDisplayLink::callback(CFRunLoopTimer, void) ()
    #19 0x0122a383 in CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION ()
    #20 0x0122b814 in __CFRunLoopDoTimer ()
    #21 0x011841e6 in __CFRunLoopRun ()
    #22 0x01183750 in CFRunLoopRunSpecific ()
    #23 0x01183671 in CFRunLoopRunInMode ()
    #24 0x021280c3 in GSEventRunModal ()
    #25 0x02128188 in GSEventRun ()
    #26 0x002aac29 in UIApplicationMain ()
    #27 0x00001c5b in main ()
    #28 0x00001c05 in start ()

Owner

Something's not being retained. I'll take a look!

Contributor
Kabal commented Oct 10, 2011

I believe this is because you are removing items from the dictionary WHILE you are enumerating through it.

I fixed it by changing the method to this:

- (void)_cleanupCells
{
    const CGRect visibleBounds = { self.contentOffset, self.bounds.size };

    NSMutableArray *cellToRemove = [[NSMutableArray alloc] init];

    [_visibleCells enumerateKeysAndObjectsUsingBlock:^(KKIndexPath *indexPath, KKGridViewCell *cell, BOOL *stop) {
        if (!KKCGRectIntersectsRectVertically(cell.frame, visibleBounds)) {
            [cellToRemove addObject:indexPath];
        }
    }];

    for (KKIndexPath *path in cellToRemove) {
        KKGridViewCell *cell = [_visibleCells objectForKey:path];
        [cell removeFromSuperview];
        [_visibleCells removeObjectForKey:path];
        [self _enqueueCell:cell withIdentifier:cell.reuseIdentifier];
    }
    [cellToRemove release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment