Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 18, 2018, 03:47:16 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Program 17.1 under Lion  (Read 1862 times)
Hero Member
Posts: 3114

« on: August 01, 2011, 01:09:36 PM »

Apparently, Apple has made a change to the compiler with the release of Lion so that all NSNumber objects now have retain counts of 0x7fffffffffffffff (similar to what's done with constant string objects).   As a result, Program 17.1 will not produce the correct results.  So use a different instead of NSNumber.   Here, for example, is Program 17.1 that uses Fraction objects instead to illustrate the point about reference counts:

Code: (Objective-C)
//  main.m
//  prog17
//  Created by Stephen Kochan on 8/1/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.

#import <Foundation/Foundation.h>
#import "Fraction.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFract = [[Fraction alloc] init];
Fraction *myFract2;
NSMutableArray *myArr= [NSMutableArray array];
       [myFract setTo: 1 over: 2];

NSLog(@"myFract retain count = %lx", (unsigned long) [myFract retainCount]);
[myArr addObject: myFract];
NSLog(@"after adding to array = %lx", (unsigned long) [myFract retainCount]);

myFract2 = myFract;
NSLog(@"after assignment to myInt2 = %lx", (unsigned long) [myFract retainCount]);

[myFract retain];
NSLog(@"myFract after retain = %lx", (unsigned long) [myFract retainCount]);
NSLog(@"myFract2 after retain = %lx", (unsigned long) [myFract2 retainCount]);

[myFract release];
NSLog(@"myFract after release = %lx", (unsigned long) [myFract retainCount]);

[myArr removeObjectAtIndex: 0];
NSLog(@"after removal from array = %lx", (unsigned long) [myFract retainCount]);

[pool drain];
return 0;

The discussion that follows in the text should be changed to refer to myFract and myFract2 as appropriate.


« Last Edit: August 01, 2011, 01:11:20 PM by skochan » Logged
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.