Bad_Access to nil

Discussion in 'Mac Programming' started by mdeh, Feb 23, 2009.

  1. macrumors 6502

    Joined:
    Jan 3, 2009
    #1
    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.
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #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.
     
  3. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #3
    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.

    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.
     
  4. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    A note from Apple's docs:

    From: http://developer.apple.com/releasenotes/Cocoa/FoundationOlder.html
     
  5. thread starter macrumors 6502

    Joined:
    Jan 3, 2009
    #5
    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.
     

Share This Page