views:

19

answers:

1

Hi,

I don't know what is going on here but I have the following code in my init method:

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]);

I know I should be releasing game1CustomEntityInfoControl since the superview retains the object but leave that for now.

Then in my layoutSubviews method I have:

// 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]);

This is the output:

2010-10-24 15:14:08.598 Sangaku[8592:207] retain count in init before alloc: 0

2010-10-24 15:14:08.603 Sangaku[8592:207] retain count in init after alloc: 1

2010-10-24 15:14:08.611 Sangaku[8592:207] retain count in init after adding to superview: 2

2010-10-24 15:14:08.616 Sangaku[8592:207] retain count as soon as you enter layoutSubviews: 2

2010-10-24 15:14:08.621 Sangaku[8592:207] retain count in layoutSubviews after calling subviews on contentView: 3

Look at the last line of output. How did the retain count go to 3? Does subviews internally do some allocation that is returned as autoreleased?

Thanks

+1  A: 

AFIAK you can't rely on the retainCount behaving this way. The Cocoa framework internals could be don't anything with your objects, perhaps on a different thread / in an asynchronous fashion.

darren
On this very note, what the OP needs to realize is that so long as he follows the object ownership rules, he'll never have to look at the retainCount anyway. And when he's tempted to look at retainCount, that should be a big red flag that he's doing something wrong.
jer