Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
I wonder if I may ask about something which I thought I understood. (Please disregard the title of the question...I had originally asked this, but figured it out)

The retainCount of "Replacement_Number" is 2 when the following code is implemented. I would have expected 1 for each, not 1 for "first_Number" and 2 for "replacement_number".

Code:
        first_number = [NSNumber numberWithInt: 42];
	replacement_number = [NSNumber numberWithInt: 7];
	
	NSLog(@"Retain Count: \"First Number:\" = %0X", [first_number retainCount]);
	NSLog(@"Retain Count: \"Replacement Number:\" = %0X", [replacement_number retainCount]);




Or is this something else completely.

Thank you in advance.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Don't pay attention to -retainCount. Just learn the basics of retain/release and make sure you're doing the right thing.

Cocoa does some unexpected things with retain counts for optimization purposes. In this case, numberWithInt: returns a cached NSNumber instance.
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
Don't pay attention to -retainCount. Just learn the basics of retain/release and make sure you're doing the right thing.

Cocoa does some unexpected things with retain counts for optimization purposes. In this case, numberWithInt: returns a cached NSNumber instance.

LOL!
It's an exercise in Kochan's book. I don't fully understand the significance of the cached NSNumber instance, but the exercise is supposed to flush out one's knowledge of memory management, which I am beginning to get. But, just for completeness, here is what is asked for.

What effect do you think the NSArray's replaceObjectAtIndex:withObject: method will have on the reference count of the object that is replaced in the array? What effect will it have on the object placed into the array? Write a program to test it. Then consult your documentation on this method to verify your results

And here is the full little program I wrote.

Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>

#define ARRAY_CAPACITY 10

int main (int argc, const char * argv[]) {
    NSAutoreleasePool		* pool = [[NSAutoreleasePool alloc] init];
	NSMutableArray			* myArr = [NSMutableArray arrayWithCapacity: ARRAY_CAPACITY];
	NSNumber				*first_number, *replacement_number;
	
	first_number = [NSNumber numberWithInt: 42];
	replacement_number = [NSNumber numberWithInt: 7];
	
	NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]); /** 1  **/
	NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);/**  2   **/
	
	[myArr addObject: first_number];
	
	
	NSLog(@"Add \"First Number\" to array");
	NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]);  /***2  ***/
	NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]);  /***2****/
	
	NSLog(@"Now replace \"First Number\" with \"Replacement Number\"");
	[myArr replaceObjectAtIndex:myArr.count - 1 withObject:replacement_number];
	NSLog(@"Retain Count: \"First Number:\" = %i", [first_number retainCount]); /***1****/
	NSLog(@"Retain Count: \"Replacement Number:\" = %i", [replacement_number retainCount]); /***3***/
        [pool drain];
        return 0;}

Could I press you to perhaps expound on the "cached NSNumber instance". thank you.

OK...SOLVED... I did not quite understand what kpua meant, but apparently the numbers -1 through 12 are used so often that they are pre-generated, and handed out as needed. Not sure if this is implementation or standard.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
A note from Apple's docs:

Since NSNumbers are immutable, we now cache and reuse some "popular" numbers in order to reduce allocation activity. This means that some distinct NSNumber allocation calls might return the same exact object, with a incremented reference count. Applications should not rely on getting distinct objects from separate NSNumber creation requests.

In Tiger "popular" numbers include -1..12, although this might very well change at any point, including in a software update.

From: http://developer.apple.com/releasenotes/Cocoa/FoundationOlder.html
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
A note from Apple's docs:

Eddie...a really dumb followup.
How did you find this? I googled/searched/etc etc but missed this....which seems to me to be an equally important part of writiting code.

Michael.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.