iOS 5.1中UITableView滚动时存在内存泄漏问题

3

每次滚动UITableview时,会有48个字节的内存泄漏。

负责的库:libsystem_c.dylib

负责的帧:strdup。

这仅在iOS 5.1上观察到,而在早期版本上没有发现。

是否还有其他人遇到了相同的问题?这是iOS 5.1中的一个错误吗?

代码:

- (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;
}

2
可以展示一些代码吗?可能最有趣的是cellForRowAtIndexPath:方法...你的项目中也使用ARC吗? - Vladimir
你使用了默认实现而没有自定义代码吗?它是否存在内存泄漏,或者内存会在稍后被清除(因为自动释放对象)? - calimarkus
@jaydee3,我测试了一个简单的应用程序,其中包含基本的表视图,但内存泄漏仍然存在。 - Shri Patil
我也遇到了这个问题,在某些罕见情况下,当滚动到最顶部并反弹,然后滚动到最底部时会导致几个泄漏,并且有一次它使用SIGKILL杀死了应用程序。 - Oscar Gomez
2个回答

1

这可能是由于您处理单元格标识符的方式不正确。实际上我很惊讶它为什么不会崩溃,因为您释放了 cellIndentifier, 但是在创建新单元格时引用了它(即当没有从dequeueReusableCellWithIdentifier 返回单元格时)。

使用单元格标识符的标准/接受的方式是使用静态标识符(因为它永远不会改变,并且只分配一次而不是潜在地100多次,因为当滚动表时,将不断调用 cellForRowAtIndexPath)。这将使您的代码更加高效。

例如:

- (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];
    }   

   ...
}

你能试着改变cellIdentifier,看看是否还会出现内存泄漏吗?


嗨,我尝试使用静态NSString作为cellIdentifier,但当tableView滚动时仍然出现相同的内存泄漏问题。 - Shri Patil
你看到哪个类/类型在泄漏吗?Instruments 显示了什么? - calimarkus

0

我认为你遇到了iOS 5.1上已经报告过的问题。我自己也遇到了这个问题。目前我还没有在苹果论坛中找到关于这个问题的链接。


我也找到了一些参考资料,表明这是iOS 5.1中已知的问题之一。其中一个参考文献:http://openradar.appspot.com/11081198 - Shri Patil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接