PDA

View Full Version : Bad_Access to nil




mdeh
Feb 23, 2009, 07:18 PM
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".


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
Feb 23, 2009, 08:35 PM
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
Feb 23, 2009, 08:48 PM
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.

#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
Feb 23, 2009, 11:01 PM
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
Feb 24, 2009, 07:07 AM
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.