NSObject retainCount question

NSObject retainCount question

Post by guoxiaotia » Sat, 29 Apr 2006 16:05:38


Hi all,
Very new to objectiveC, and puzzled by the out output of following
program:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
N *** ableArray* array = [[N *** ableArray alloc] init];

int i;
for(i = 0; i < 20; i++)
{
NSNumber* newNumber = [[NSNumber alloc] initWithInt:(i*3)];
[array addObject: newNumber];
[newNumber release];
NSLog(@"The current retain count of this number is %d", [newNumber
retainCount]);

}
[array release];
[pool release];
return 0;
}

First 5 NSNumber object has retainCount 2, and the rest of them have
retainCount 1, I was expecting 1 for all of them:

2006-04-27 23:59:31.376 Lottery[1180] The current retain count of this
number is 2
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 2
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 2
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 2
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 2
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.377 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.378 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.378 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.378 Lottery[1180] The current retain count of this
number is 1
2006-04-27 23:59:31.378 Lottery[1180] The current retain count of this
number is 1


Could anyone please explain this? thanks in advance!

-Xiaotian
 
 
 

NSObject retainCount question

Post by Gregory We » Sat, 29 Apr 2006 20:17:03

In article < XXXX@XXXXX.COM >,



Would you be satisfied with the answer that the retain count is an
implementation detail that almost no application developer should ever
have any reason to care about? It may be, for example, that because
NSNumber is immutable instances are cached by the system and shared when
NSNumbers with duplicate values are requested. So somewhere else in your
process but independent of your code several low-integer NSNumbers are
already in existence. (In theory, your array might be responsible for
some of that.)

--
"Congurutulation!!!" - The subject line on some spam I received recently.
I have no idea what it means, but it's such a cool "word" (by which I mean
pronouncable sequence of letters) regardless.

 
 
 

NSObject retainCount question

Post by Gregory We » Sat, 13 May 2006 02:13:11

In article < XXXX@XXXXX.COM >,



I know it's been a while, but I wanted to follow up my earlier response
about the possibility of numbers getting cached. I was reading the 10.4
CoreFoundation release notes yesterday and ran across a comment that the
integers from -1 to 12 are, in fact, pre-created and cached because they
are so commonly used.

--
"Congurutulation!!!" - The subject line on some spam I received recently.
I have no idea what it means, but it's such a cool "word" (by which I mean
pronouncable sequence of letters) regardless.
 
 
 

NSObject retainCount question

Post by Mark Besse » Sat, 20 May 2006 17:24:55


I seem to recall that this behavior changed in 10.4, which caused a
number of applications to fail in weird ways, because they'd been
implicitly depending on being able to create "different" instances of
the same NSNumber. Amusingly, this included some of Apple's own
developer tools...

-Mark
 
 
 

NSObject retainCount question

Post by Gregory We » Sun, 21 May 2006 09:47:36

In article < XXXX@XXXXX.COM >,




I have to say I'm a bit surprised that anyone would either expect or
need to be able to do so. I may be tired, but I can't imagine what
benefit someone would get from having two distinct but equivalent
immutable objects that are conceptually atomic like that.

--
"Congurutulation!!!" - The subject line on some spam I received recently.
I have no idea what it means, but it's such a cool "word" (by which I mean
pronouncable sequence of letters) regardless.
 
 
 

NSObject retainCount question

Post by Michael As » Sun, 21 May 2006 13:24:40


Maybe I'm overly cynical but I can't say I'm surprised. The crazy stuff
that programmers (even, nay, especially myself) end up writing can be
impressively wrong.

Usually it all made sense at the time....

--
Michael Ash
Rogue Amoeba Software
 
 
 

NSObject retainCount question

Post by Gregory We » Sun, 21 May 2006 21:30:29

In article < XXXX@XXXXX.COM >,





I suppose. I can't recall ever having made that particular assumption,
but I have done my share of "made sense at the time" things.

--
"Congurutulation!!!" - The subject line on some spam I received recently.
I have no idea what it means, but it's such a cool "word" (by which I mean
pronouncable sequence of letters) regardless.
 
 
 

NSObject retainCount question

Post by lally.sing » Tue, 23 May 2006 21:08:27

To be fair, all it's assuming is that every time you create a new
object, it has a unique memory address.
 
 
 

NSObject retainCount question

Post by Gregory We » Tue, 23 May 2006 21:50:48

In article < XXXX@XXXXX.COM >,



I understand that that's the assumption. I'm just saying I'm not sure
why you'd assume that or, perhaps more importantly, what the value would
be in being able to. Why would someone want to be able to differentiate
this immutable object that happens to represent the integer 3 from this
other immutable object that happens to represent the integer 3?

--
"Congurutulation!!!" - The subject line on some spam I received recently.
I have no idea what it means, but it's such a cool "word" (by which I mean
pronouncable sequence of letters) regardless.