kochan prog17.2

Discussion in 'Mac Programming' started by jamesapp, May 3, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    trying to write a program from a book by Stephen Kochan
    the program compiled ok but my output doesen't match the output from the book.
    here is my testfile which i called prog17.2.m

    Code:
    // reference counting with string objects
    
    #import <Foundation/NSObject.h>
    #import <Foundation/NSAutoreleasePool.h>
    #import <Foundation/NSString.h>
    #import <Foundation/NSArray.h>
    #import <stdio.h>
    
    int main (int argc, char *argv[])
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      NSString *myStr1 = @"Constant string";
      NSString *myStr2 = [NSString stringWithString:
                              @"string 2"];
      NSMutableString *myStr3 = [NSMutableString stringWithString:
                              @"string 3"];
      NSMutableArray *myArr = [NSMutableArray array]; 
                                  
      printf ("Retain count: myStr1: %x, myStr2: %x, myStr3: %x\n",
               [myStr1 retainCount], [myStr2 retainCount],
               [myStr3 retainCount]);
               
      [myArr addObject: myStr1];
      [myArr addObject: myStr2];
      [myArr addObject: myStr3];
      
      printf ("Retain count: myStr1: %x, myStr2: %x, myStr3: %x\n",
               [myStr1 retainCount], [myStr2 retainCount],
               [myStr3 retainCount]);
      
               
      [myStr1 retain];
      [myStr2 retain];
      [myStr3 retain];
      
      printf ("Retain count: myStr1: %x, myStr2: %x, myStr3: %x\n",
               [myStr1 retainCount], [myStr2 retainCount],
               [myStr3 retainCount]);
               
      // bring the reference count of myStr2 and myStr3 back down to 2
      
      [myStr2 release];
      [myStr3 release];
      
      [pool release];
      return 0;
    }
    
    output from my program

    Code:
    james-collinss-macbook-pro:prog17 jamescollins$ ./prog17.2
    Retain count: myStr1: 7fffffff, myStr2: 7fffffff, myStr3: 1
    Retain count: myStr1: 7fffffff, myStr2: 7fffffff, myStr3: 2
    Retain count: myStr1: 7fffffff, myStr2: 7fffffff, myStr3: 3
    
    output from book:


    Code:
    Retain count myStr1: ffffffff, myStr2: 1, myStr3: 1
    Retain count myStr1: ffffffff, myStr2: 2, myStr3: 2
    Retain count myStr1: ffffffff, myStr2: 3, myStr3: 3
    
    just wondering what i am doing wrong (if anything)?
     
  2. Gelfin macrumors 68020

    Gelfin

    Joined:
    Sep 18, 2001
    Location:
    Denver, CO
    #2
    I don't think you're doing anything wrong. Unless I miss my guess you are bumping into some under-the-hood refinements. It seems it is being smarter about references to static strings (there was never any good reason for myStr2 to have been a copy there).

    As far as the difference between 0x7fffffff and 0xffffffff, the retainCount documentation still says that retainCount should always return UINT_MAX for static objects, but it does seem to return INT_MAX in some cases, and I haven't ever seen a good explanation why. The good news is you can take the INT_MAX result as demonstrating the same thing Kochan meant it to: that the object pointed to isn't going to go away by releasing it.

    This example pokes around internal object management details you typically won't need to fiddle with in practice, and that may change slightly from time to time. I wouldn't worry about the difference in your results. If you were using the same OS and XCode version Kochan was, you'd get the same results.
     

Share This Page