为什么这个NIB视图在返回之前没有释放?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ThemeCell" owner:self options:nil];
cell = self.themeCell;
self.themeCell = nil;
}
...
return cell;
我的理解是 self.themeCell = nil; 应该销毁该对象,因为它不再有任何所有者。 cell = self.themeCell
不会保留它,而只是分配它。那么是什么让细胞保持活力呢?我只能假设合成的属性会自动释放旧值,而不是立即释放它。是这样吗?
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ThemeCell" owner:self options:nil];
cell = self.themeCell;
self.themeCell = nil;
}
...
return cell;
My understanding is that self.themeCell = nil;
should destroy the object since there is no longer any owner of it. cell = self.themeCell
doesn't retain it, but just assigns it. So what keeps the cell alive? I can only assume that the synthesized property is autoreleasing the old value instead of releasing it immediately. Is this the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
nib 加载过程稍微(但不是非常)复杂,并且 OSX 和 iPhone 平台之间有所不同。您可以在 资源编程指南的 Nib 对象生命周期部分。在表 1-1 中您会发现:
因此,会发生的情况是,创建单元格时保留计数为 1,然后当使用合成的 setter 设置它时,它增加到 2。当您将属性设置为
nil
时,保留计数会增加到 2。 count 减至 1,单元格返回到表视图。表视图将其添加到其视图层次结构中,从而保留它(也可能在其逻辑的其他部分保留它)。在这一切之后,autorelease
池被耗尽。不,合成的 setter 会立即释放对象。 (尽管某些框架类可能会保留该对象更长时间,例如,如果它是一个需要动画显示的视图。)
The nib loading process is slightly (but not very) complicated, and differs between the OSX and iPhone platforms. You can read about this in the Nib Object Life Cycle section of the Resource Programming Guide. In table 1-1 you will find this:
So what happens is that the cell is created with a retain count of 1, and then when it is set with your synthesized setter, that it increased to 2. When you set your property to
nil
the retain count goes down to 1, and the cell is returned to the table view. The table view adds it to its view hierarchy, and thereby retains it (and maybe retains it in other parts of its logic as well). After all this, theautorelease
pool is drained.No, synthesized setters release the object immediately. (Although some framework classes might hold on to the object a bit longer, if it's a view that needs to be animated out for example.)
AFAIK,合成属性使用
release
,而不是autorelease
。单元格可能有一个委托或在 NIB 中指定的保留所有权的委托,或者在 loadNibNamed:owner:options: 调用中设置了仍在挂起的对象上的自动释放。在
self.themeCell=nil;
前后放置NSLog(@"Retain Count: %d", [cell keepCount]);
进行验证。AFAIK, synthesized properties use
release
, notautorelease
.It may be that the cell has a delegate or such specified in the NIB which is retaining ownership, or something in the
loadNibNamed:owner:options:
call sets an autorelease on the object that is still pending. PutNSLog(@"Retain Count: %d", [cell retainCount]);
before and after theself.themeCell=nil;
to verify.