Mah TableView be a'crashin'.

Discussion in 'iOS Development' started by Cyanidepoison, May 10, 2009.

  1. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    So I've got this fun little method:
    Code:
    - (void)initFiles {
    	NSArray *sandbox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    	documents = [sandbox objectAtIndex:0];
    	NSFileManager *fm = [NSFileManager defaultManager];
    	files = [NSMutableArray arrayWithArray:[fm directoryContentsAtPath:documents]];
    }
    
    documents is an NSArray, files is an NSMutableArray, if you didn't catch that.


    Now, I've got a nice little tableview to display this array.
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
    	cell.text = [files objectAtIndex:indexPath.row];
    	return cell;
    }
    
    (I switched all the stuff back to the 2.x cell text instead of 3.0 to keep it kosher)

    Now, this all works fine and dandy, but it crashes when I decide to do a big scroll of the table, past the array bounds about five cells.


    Here is a backtrace, just for kicks.
    Code:
    #0  0x91497688 in objc_msgSend ()
    #1  0x0000256f in -[RootViewController tableView:cellForRowAtIndexPath:] (self=0xd18e60, _cmd=0x319a5c64, tableView=0x101c400, indexPath=0xd2c2e0) at /Users/ryanworl/Documents/Downloads/Classes/RootViewController.m:96
    #2  0x30944745 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] ()
    #3  0x3093df10 in -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] ()
    #4  0x3094fdb5 in -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow] ()
    #5  0x309472d9 in -[UITableView layoutSubviews] ()
    #6  0x00b41d98 in -[CALayer layoutSublayers] ()
    #7  0x00b41b59 in CALayerLayoutIfNeeded ()
    #8  0x00b413b2 in CA::Context::commit_transaction ()
    #9  0x00b41026 in CA::Transaction::commit ()
    #10 0x00b492e4 in CA::Transaction::observer_callback ()
    #11 0x30245c32 in __CFRunLoopDoObservers ()
    #12 0x3024503f in CFRunLoopRunSpecific ()
    #13 0x30244628 in CFRunLoopRunInMode ()
    #14 0x32052c31 in GSEventRunModal ()
    #15 0x32052cf6 in GSEventRun ()
    #16 0x30901f2e in UIApplicationMain ()
    


    I'm stumped as to why it crashes. I do stuff with tableviews all the time and scrolling past the bounds of the array that is the datasource never caused any problems, it just didn't draw new cells.

    Anyone have any insight on why this would happen?
  2. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    Look like you didn't retain files

    files = [[NSMutableArray arrayWithArray:[fm directoryContentsAtPath:documents]] retain];

    make sure you release files too

    that is autoreleased objects so it get released very early.
  3. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    Winner, winner, chicken dinner.

    Ref count FTL.
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Excuse me but that's my brother your eating......
  5. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    like this one -

    Please Register or Log in to view images

  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Here's the line causing the errr.
    Code:
    cell.text = [files objectAtIndex:indexPath.row];
    ;

    If the indexPath.row is greater than [files count] - 1, then you will be out of bounds.

    You *should* have a check in there somewhere that only does that command if it can. Ie,
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    	if (!(indexPath.row > [files count] -1))
    		cell.text = [files objectAtIndex:indexPath.row];
    	return cell;
    }

Share This Page