我不知道这里发生了什么,但我的 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 ,因为超级视图保留了该对象,但暂时保留它。
然后在我的 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] 在分配前在 init 中保留计数: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 的?子视图是否在内部执行一些作为自动释放返回的分配?
谢谢