Possible Duplicate:
NSNumber retain count issue
Hello,
I have the following code:
NSNumber *number = [NSNumber numberWithInt:5];
int i = [number retainCount];
[number release];
i = [number retainCount];
[number release];
i = [number retainCount];
the problem is that in line 2 , the value of parameter i is 2
and in line 4 the value is 1.
then in line 6 the value is still 1.????????
first i dont understand why after init *number the retaincount is 2 and not 1??
second i dont understand why after release it 2 times retaincount is not 0?
it doesnt matter how many times i release the object the retaincount stay 1.
The problem is that you should never look at the ‘retainCount’ of an object unless you really know what you’re doing, it’ll only confuse you.
What’s going on is that NSNumber is doing something behind the scenes. I’m not sure what, and I usually don’t care. If I create an abject, I’m responsible for releasing it. As long as I fulfill my responsibilities, everything will work out as it should.
In your specific example, you’re getting a reference to an ‘NSNumber’ and releasing it twice. Since you don’t own that object, you should not be releasing it at all.
To clarify, the reason you shouldn’t be looking at retain counts is that it frequently will mislead you. To quote @chuck from the link in the comments.