Shenjun
Shenjun

Reputation: 103

UITableView's memory leaking issue

Hi All I have a UITableView with the list of items fetched from sqllite. But there's a memory leak when rendering the view. Following is my cellForRowAtIndexPath method.

static NSString *CellIdentifier = @"BarListItemCell";


BarListItemViewCell *cell = (BarListItemViewCell *)[tableView 
                                  dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
    NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"BarListItemViewCell" owner:self options:nil];
    for (id cellObject in nib) {
        if ([cellObject isKindOfClass : [BarListItemViewCell class]]) {
            cell = (BarListItemViewCell *) cellObject;
            //break;
        }
    }
    NSString * key =   [keys objectAtIndex:[indexPath section]];
    NSDictionary * unit = [[barListDataSource objectForKey:key] objectAtIndex:[indexPath row]];

    NSLog(@"unit count is %d", [unit retainCount]);

    cell.name.text = [unit objectForKey:@"name"];
    cell.address.text = [unit objectForKey:@"address1"];
    cell.features.text = [unit objectForKey:@"features"];
    cell.logo.image = [UIImage imageWithData:[unit objectForKey:@"logo"]];  
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}    
return cell;

You can see the line "NSLog(@"unit count is %d", [unit retainCount]);". It's very strange that after viewDidLoad, the console displays 3 lines "unit count is 2" (I have 3 items in the whole screen). But when I drag the screen to let the UITableView show the next item, the console displays "unit count is 1". When the [tableView reloadData] method is called, the console displays also "unit count is 1". So it seems that the UITableView will release the datasource automatically. That's why I keeps memory reference count of unit being 2, otherwise, over releasing will happen. But the cost is that the memory occupied by unit will never be freed!

Upvotes: 1

Views: 807

Answers (1)

bbum
bbum

Reputation: 162722

Do not call -retainCount.

The absolute retain count of an object is meaningless.

You should call release exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).

See the Memory Management Guidelines for full details.


There is nothing strange about the retain count of unit from what you describe. Your code appears to be printing the retain count of an object that may or may not have been created during the current pass through the event loop and, thus, might have been retain/autoreleased multiple times. Or not. It doesn't matter.

What does matter is what the Allocations Instrument shows. Do you see an accretion of objects over time? If so, what are they and what is retaining them (or not releasing them)?

If you do, it might be the case that Heapshot analysis would prove useful to you.

Upvotes: 1

Related Questions