我创建了一个带有自定义笔尖的 UITableViewCell 子类,并在我的应用程序的两个不同的 UITableViews 中使用它。它在其中一个表中完美运行,但是当我大力滚动时另一个表崩溃。Instruments 在这段代码中识别出一个僵尸(在 cellForRowAtIndexPath 中):
NSString *identifier = @"edit";
LogTableCell *cell = (LogTableCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = (LogTableCell*) [[[NSBundle mainBundle] loadNibNamed:@"LogTableCell" owner:self options:nil] objectAtIndex:0];
[cell retain]; // prevents zombies!
}
NSLog(@"%@: retainCount: %d", identifier, [cell retainCount]);
// some other cell init stuff
return cell;
注意[单元保留];行 - 当它在那里时,代码可以流畅地工作。把它拿出来,然后崩溃。NSLog 总是报告一个 retainCount 为 2,所以它不应该是必要的。但如果我做这样的事情:
if ([cell retainCount] < 1) { [cell retain]; } // does not prevent zombies!
它不起作用。没有 alloc/init,所以我根本不需要自动释放或担心这个,我一直认为 cellForRowAtIndexPath 会为我释放单元格。
当我不使用 Instruments 时,这是我从 xcode 得到的错误:
*** -[CALayer retain]: message sent to deallocated instance 0x4d8e930
即使它适用于 [细胞保留]; 行,它看起来像是分析(和我)的泄漏,所以我想解决这个问题。有人知道这里发生了什么吗?