简体   繁体   中英

Memory leak in UITableView when scrolled on iOS 5.1

For each time UITableview is scrolled, there is a memory leak of 48 bytes. Responsible library : libsystem_c.dylib Responsible frame : strdup.

This is observed only on iOS 5.1 and not on earlier versions. Did anyone else faced the same? Is this a bug in iOS 5.1?

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath 
{
    NSString *cellIdentifier = [[NSString alloc] initWithString:@"fontSelectionCell"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    [cellIdentifier release];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }       

    cell.textLabel.text = [fontNameList objectAtIndex:[indexPath row]];
    cell.selectionStyle =UITableViewCellSelectionStyleGray;
    cell.textLabel.font = [UIFont systemFontOfSize:17.0];

    if ([fontName isEqualToString:cell.textLabel.text])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = [UIColor blueColor];

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = [UIColor blackColor]; 
    }

    return cell;
}

It could be due to the way you are handling the cell identifier. I'm actually surprised it does not crash for you, since you release cellIndentifier but then reference it when creating a new cell (ie when a cell wasn't return for reuse from dequeueReusableCellWithIdentifier ).

The standard/accepted way to use a cell identifier is to use a static (because it won't ever change, and it will only be alloc-ed once and not potentially 100s of times since cellForRowAtIndexPath is called constantly when scrolling a table). This would make your code much more efficient.

ie

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *cellIdentifier = @"fontSelectionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }   

   ...
}

Could you try changing cellIdentifier and see if you still get the leak?

I think you are having this issue that was already report on iOS 5.1. I am having that myself too. At the moment I wasn't able to find the link in apple's forums concerning this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM