为什么这段代码会调用自动释放池?

发布于 2024-11-07 09:46:12 字数 1204 浏览 4 评论 0原文

我看到以下日志...

“__NSAutoreleaseNoPool(): UITableViewCellContentView 类的对象 0x58264b0 自动释放,没有池 - 只是泄漏”

这是一个巨大的发布池日志,上面只是我复制的发布日志之一。我

有一个 CustomCell,它根据业务逻辑向自身添加图块。但问题是当我调用单元格的创建时,我看到了上面的日志消息。我没有发现我的代码有任何问题。有人对此有任何线索吗?

- (UITableViewCell *) tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    UnSavedNoteListCell *cell;
    NSString *CellIdentifier = [@"Cell_" stringByAppendingString:[NSString stringWithFormat:@"%d", indexPath.row]];
     cell = (UnSavedNoteListCell *)[inTableView  dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) 
{

    NSMutableArray *cellProgressNoteCollection = [self getcellProgressNoteCollectionForLandScape:indexPath];
    cell = [[[UnSavedNoteListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier cellTiles:cellProgressNoteCollection] autorelease];
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

}

//    cell.textLabel.text = [NSString stringWithFormat:@"%d", rand()];
//    cell.textLabel.textColor = [UIColor redColor];
// Configure the cell...

return cell;
}

I am seeing the following log...

"__NSAutoreleaseNoPool(): Object 0x58264b0 of class UITableViewCellContentView autoreleased with no pool in place - just leaking "

It is a huge release pool log, above is just one of the release log that i copied...

I have a CustomCell that adds tiles into itself depending on the business Logic. But the problem is when I call the creation of the cell, i see the above log messages. I don't see anything wrong with my code.. Does any one has any clue about it?

- (UITableViewCell *) tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    UnSavedNoteListCell *cell;
    NSString *CellIdentifier = [@"Cell_" stringByAppendingString:[NSString stringWithFormat:@"%d", indexPath.row]];
     cell = (UnSavedNoteListCell *)[inTableView  dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) 
{

    NSMutableArray *cellProgressNoteCollection = [self getcellProgressNoteCollectionForLandScape:indexPath];
    cell = [[[UnSavedNoteListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier cellTiles:cellProgressNoteCollection] autorelease];
    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

}

//    cell.textLabel.text = [NSString stringWithFormat:@"%d", rand()];
//    cell.textLabel.textColor = [UIColor redColor];
// Configure the cell...

return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-11-14 09:46:12

我注意到的一件事是,您没有重用任何 uitableviewcell,因此您的性能可能会受到很大影响。在上面的代码中,您为每一行创建一个新的单元格标识符。尝试这样做:

- (UITableViewCell *) tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSString *CellIdentifier = @"Cell";
    UnSavedNoteListCell  *cell = (UnSavedNoteListCell *)[inTableView  dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        NSMutableArray *cellProgressNoteCollection = [self getcellProgressNoteCollectionForLandScape:indexPath];
        cell = [[[UnSavedNoteListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier cellTiles:cellProgressNoteCollection] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    }

    return cell;
}

这样,您的表视图可以维护标记为重用的表单元格列表。

One thing I do notice is that you're not reusing any of your uitableviewcells, so you're probably taking on a big performance hit. In your code above, you're creating a new cell identifier for every row. Try this instead:

- (UITableViewCell *) tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    NSString *CellIdentifier = @"Cell";
    UnSavedNoteListCell  *cell = (UnSavedNoteListCell *)[inTableView  dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        NSMutableArray *cellProgressNoteCollection = [self getcellProgressNoteCollectionForLandScape:indexPath];
        cell = [[[UnSavedNoteListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier cellTiles:cellProgressNoteCollection] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    }

    return cell;
}

This way, your tableview can maintain a list of tablecells that are marked for reuse.

秋叶绚丽 2024-11-14 09:46:12

更好的问题是为什么你没有自动释放池。

以下是您在该代码中进行的一些可能需要自动释放池的调用。 (实际上,其中一些可能没有。):

  • stringByAppendingString
  • stringWithFormat
  • dequeueReusableCellWithIdentifier
  • autorelease
  • getcellProgressNoteCollectionForLandScape< /code> (如果按照约定编写)
  • clearColor (理论上,但可能不是)

UIKit 需要一个自动释放池。

我发现您有两个潜在原因:

  1. 您的自动释放池消失了。 (它去了哪里?)
  2. 这是从主线程以外的线程调用的。

A better question is why you don't have an autorelease pool.

Here's some of the calls you make in that code that might require an autorelease pool. (Some of them probably don't, actually.):

  • stringByAppendingString
  • stringWithFormat
  • dequeueReusableCellWithIdentifier
  • autorelease
  • getcellProgressNoteCollectionForLandScape (if it's written per conventions)
  • clearColor (theoretically, but probably not)

UIKit requires an autorelease pool.

You have two potential causes that I see:

  1. Your autorelease pool is gone. (Where did it go?)
  2. This is being called from a thread other than the main thread.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文