为什么这个NIB视图在返回之前没有释放?

发布于 2024-09-12 15:19:28 字数 436 浏览 6 评论 0原文

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 技术交流群。

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

发布评论

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

评论(2

痞味浪人 2024-09-19 15:19:28

nib 加载过程稍微(但不是非常)复杂,并且 OSX 和 iPhone 平台之间有所不同。您可以在 资源编程指南的 Nib 对象生命周期部分。在表 1-1 中您会发现:

nib文件中的对象已创建
保留计数为 1,然后
自动释放。当它重建
然而,对象层次结构,UIKit
重新建立之间的连接
使用 setValue:forKey: 的对象:
方法,该方法使用可用的
setter 方法或通过以下方式保留对象
如果没有 setter 方法则默认
可用

因此,会发生的情况是,创建单元格时保留计数为 1,然后当使用合成的 setter 设置它时,它增加到 2。当您将属性设置为 nil 时,保留计数会增加到 2。 count 减至 1,单元格返回到表视图。表视图将其添加到其视图层次结构中,从而保留它(也可能在其逻辑的其他部分保留它)。在这一切之后,autorelease池被耗尽。

我只能假设综合
属性正在自动释放旧的
value而不是release,这是
案例?

不,合成的 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:

Objects in the nib file are created
with a retain count of 1 and then
autoreleased. As it rebuilds the
object hierarchy, however, UIKit
reestablishes connections between the
objects using the setValue:forKey:
method, which uses the available
setter method or retains the object by
default if no setter method is
available

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, the autorelease pool is drained.

I can only assume that syntesized
property is autoreleasing the old
value instead of release, is this the
case?

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.)

一梦浮鱼 2024-09-19 15:19:28

AFAIK,合成属性使用 release,而不是 autorelease

单元格可能有一个委托或在 NIB 中指定的保留所有权的委托,或者在 loadNibNamed:owner:options: 调用中设置了仍在挂起的对象上的自动释放。在 self.themeCell=nil; 前后放置 NSLog(@"Retain Count: %d", [cell keepCount]); 进行验证。

AFAIK, synthesized properties use release, not autorelease.

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. Put NSLog(@"Retain Count: %d", [cell retainCount]); before and after the self.themeCell=nil; to verify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文