UITableViewController Memory Issue? Or Bug?

Discussion in 'iOS Programming' started by Littleodie914, Oct 17, 2010.

  1. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #1
    Hey guys! I posted this question to StackOverflow as well, but I figured you might be able to beat them to the punch. :) I'm working with an NSTableViewController, and I have a section of code as follows.

    Code:
    Subject *subject = [[context subjects] objectAtIndex:[indexPath row]];
    
    cell.textLabel.text = [subject name];
    cell.showsReorderControl = YES;
    
    return cell;
    This code is in the UITableViewDataSource method that returns an UITableViewCell for an index path. (I'm sure you're all familiar with it.)

    I'm noticing a strange bug where even though [subject name] is returning "Subject 16", the cell is actually being displayed with "Subject 11". I have no clue what's going on, I have placed breakpoints and used GDB to examine the code at that location. See the attached screenshots, the first is a breakpoint as I was scrolling to the last row, where the issue occurs. The second is after the code has executed, and the cell is being displayed. (This bug seems to occur to random rows, only one at a time, and it takes some random clicking, re-ordering, etc. to coax it out.)

    Also, I'm wondering if there's maybe a memory issue, as indicated by the third screenshot. For some reason, the delete control isn't being displayed for the offending cell. Examining the object in GDB I can't seem to find any memory issues though. :(

    Thanks guys!
     

    Attached Files:

  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Can anything ever be nil? The name of a subject for example?
     
  3. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #3
    The name of the Subject can be nil, but at no point in the code is it set to nil.

    The name ivar is defined as a property as follows:

    Code:
    @property (nonatomic, copy) NSString *name;
    Edit: Plus, if the name was nil, wouldn't the textLabel read "(null)"?
     
  4. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #4
    Things are getting a little weirder the more I poke around:
     

    Attached Files:

  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    By which, of course, you really meant UITableViewController. :D

    Based on the symptoms, it sounds to me like you are having issues with the way the table view controller caches cells and older, cached cells being presented without updated data.
     
  6. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #6
    You got it. :D

    That's ultimately the conclusion I came to earlier today. Each object in my model (for secret reasons :p) has a unique identifier, so I just create each UITableViewCell with that same unique identifier. This way each cell is associated with a single data object.

    It seemed to have solved the issues, but honestly, defeats the purpose of caching the cells. If I'm properly updating the view for each cell, how could this be working in anyone else's application? :confused:
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Maybe you're not properly updating the cell.

    For example, I only see code that sets a couple of things in a cell, even though that cell may still have some attributes set from its previous use. Try resetting all the visible attributes of a cell before setting new ones. I refer you to this in the class reference doc for UITableViewCell:

    The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell. (Emphasis in original.)​
    http://developer.apple.com/library/...ITableViewCell_Class/Reference/Reference.html

    Have you looked at the source of any of the sample projects associated with UITableViewCells?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Yeah, I think without seeing your entire tableView:cellForRowAtIndexPath: and without knowing how [context subjects] is setup and populated, it's hard for us to diagnose the problem.
     
  9. Littleodie914 thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #9
    I appreciate the help, and I apologize for not being able to post the entire project. Here's the cell: code.

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    	Subject *subject = [[context subjects] objectAtIndex:[indexPath row]];
    	
        NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    	
    	// Configure the cell.
    	cell.textLabel.text = subject.name;
    	cell.showsReorderControl = YES;
    
        return cell;
    }
    
    The context contains an array of Subject objects. Each Subject has a property:

    Code:
    @property (nonatomic, copy) NSString *name;
    By changing the cell code to use:

    Code:
    NSString *CellIdentifier = subject.uuid;
    
    The problem appears to go away, but it's very difficult to reproduce in the first place. :eek:
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Yeah, that code seems to be fine to me. Therefore, I would begin to suspect an issue with either context or subjects. I would apply some basic debugging to ensure they are working the way they are expected to. Also, you may have a memory-management issue where something is over-released and that memory re-assigned but a pointer still referencing it. Something to consider at least.
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Run Instruments and enable zombies.
     

Share This Page