在单元格的contentView上调用“subviews”方法会增加保留计数

时间:2010-10-24 22:17:36

标签: objective-c retaincount

我不知道这里发生了什么,但我的init方法中有以下代码:

NSLog(@"retain count in init before alloc: %d", [game1CustomEntityInfoControl retainCount]);

game1CustomEntityInfoControl = [[CustomEntityInfoControl alloc] initWithFrame:CGRectZero];

NSLog(@"retain count in init after alloc: %d", [game1CustomEntityInfoControl retainCount]);

[[self contentView] addSubview:game1CustomEntityInfoControl];

NSLog(@"retain count in init after adding to superview: %d", [game1CustomEntityInfoControl retainCount]);

我知道我应该发布game1CustomEntityInfoControl,因为superview会保留该对象,但暂时离开。

然后在我的layoutSubviews方法中我有:

// We always call this, the table view cell needs to do its own work first
[super layoutSubviews];

NSLog(@"retain count as soon as you enter layoutSubviews: %d", [game1CustomEntityInfoControl retainCount]);

[[self contentView] subviews];

NSLog(@"retain count in layoutSubviews after calling subviews on contentView: %d", [game1CustomEntityInfoControl retainCount]);

这是输出:

2010-10-24 15:14:08.598 Sangaku [8592:207]在分配前保留初始计数:0

2010-10-24 15:14:08.603 Sangaku [8592:207]在分配后在init中保留计数:1

2010-10-24 15:14:08.611 Sangaku [8592:207]在加入superview后保留在init中的计数:2

2010-10-24 15:14:08.616 Sangaku [8592:207]一进入layoutSubviews就会保留计数:2

2010-10-24 15:14:08.621 Sangaku [8592:207]在contentView上调用子视图后在layoutSubviews中保留计数:3

查看最后一行输出。保留计数如何变为3?子视图是否在内部执行一些作为自动释放返回的分配?

由于

1 个答案:

答案 0 :(得分:1)

AFIAK你不能依赖retainCount这样做。 Cocoa框架内部可能与您的对象无关,可能在不同的线程上/以异步方式。