Amazon.com Widgets 17.2 in original book question
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 01:39:38 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Answers to Exercises
| | |-+  Chapter 17
| | | |-+  17.2 in original book question
Pages: [1]   Go Down
Print
Author Topic: 17.2 in original book question  (Read 793 times)
speedsimmons
Newbie
*
Posts: 10






« on: April 12, 2010, 08:34:52 PM »

#import <Foundation/Foundation.h>


int main (int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   NSMutableArray *myArr = [NSMutableArray array];
   NSNumber *objA = [NSNumber numberWithInt: 10];
   NSNumber *objB = [NSNumber numberWithLong: 14000099];
   NSMutableString *myStr = [NSMutableString stringWithString: @"this is cool"];
   
   NSLog(@"The count for myArr after declaring it is %x\n", [myArr retainCount]);
   NSLog(@"The count for objA after initializing it with an INT is %x (why is this?)\n", [objA retainCount]);
   NSLog(@"The count for objB after initializing it with a LongINT is %x\n", [objB retainCount]);
   NSLog(@"The count for myStr after declaring it as a mutable string is %x\n\n", [myStr retainCount]);

   //adding A and B to the array collection
   [myArr addObject: objA];
   [myArr addObject: objB];
   
   NSLog(@"The count for myArr after adding two objects to it is %x\n", [myArr retainCount]);
   NSLog(@"The count for objA after being added to the array is %x\n", [objA retainCount]);
   NSLog(@"The count for objB after being added to the array is %x\n\n", [objB retainCount]);
   
   //removing objA, and putting in myStr
   [myArr replaceObjectAtIndex: 0 withObject: myStr];
   
   NSLog(@"The count for myArr after removing and object and replacing it with another is %x\n", [myArr retainCount]);
   NSLog(@"The count for objA after it was replaced is %x\n", [objA retainCount]);
   NSLog(@"The count for objB after nothing was done to it is %x\n", [objB retainCount]);
   NSLog(@"The count for myStr after being added to the array is %x\n\n", [myStr retainCount]);
   
   [objA release];
   [objB release];
   [myStr release];
   
   NSLog(@"The count for objA after it was released is %x\n", [objA retainCount]);
   NSLog(@"The count for objB after it was released is %x\n", [objB retainCount]);
   NSLog(@"The count for myStr after it was released is %x\n\n", [myStr retainCount]);

   NSLog(@"I 'thought' the pool would deal with all these types of objects, but I guess not?" );
   NSLog(@"Shouldn't it have brought their counts down to ZERO, which would trigger deallocation?" );

   // If i comment out the pool drain, it doesn't error out... what's the deal?
   [pool drain];
   
    return 0;
}



so, I'm sorta confused here, i totally 'thought' I understood the theories in this chapter, but it doesn't seem to work out like I thought it would, so maybe I'm missing something.

First, how come objA is taking up 2 spots? when objB takes up only one, and the only difference it the int/longInt
Second, how come if they are all at "1" before we get to the [pool drain], it errors out?
Third, how come it 'works' when i don't include the [pool drain] (my guess is that it is actually NOT working, and im leaking memory...)

It works if I only use strings.  what's the deal with NSNumber?

Thanks in advance!!!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: April 13, 2010, 03:07:43 AM »

As noted in the book and elsewhere in this forum, constant string objects and NSNumber objects created for numbers -12 to 10 (I think that was the range) have special retain counts (you may want to search for that topic).

Remember that adding an object to an array increases its retain count by 1 and removing it decreases it.  Your array will be autoreleased.  So will your number and string objects.  Therefore, your retain count for any of these objects in an autoreleased array must be at least 2 when the autorealease pool is drained.

Cheers, 

Steve Kochan
Logged
speedsimmons
Newbie
*
Posts: 10






« Reply #2 on: April 13, 2010, 06:02:29 PM »

Totally got it now! Thanks for the further explanation!
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.