Amazon.com Widgets 3rd printing, page 416
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 29, 2014, 03:42:32 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
| |-+ Errata
| | |-+ 5th printing
| | | |-+ 3rd printing, page 416
Pages: [1] Go Down
Print
Author Topic: 3rd printing, page 416 (Read 1913 times)
pascalhofmann
Newbie
*
Posts: 2






on: July 14, 2009, 01:56:39 AM

Hi,
on the top of the page, I think it should be
Code: (Objective-C)
[str release]

instead of
Code: (Objective-C)
[str autorelease]

Regards

Pascal
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: July 14, 2009, 06:20:02 AM

Pascal.

No, autorelease is correct.  It's marked for later release.  If the argument that is passed into setStr: is the same value that is currently stored inside str, then a release would potentially destroy the object.  Alternatively, you could code it in this order:

Code: (Objective-C)
[s retain];
[str release];
str = s;

Cheers,

Steve Kochan
Logged
pascalhofmann
Newbie
*
Posts: 2






Reply #2 on: July 15, 2009, 03:07:19 AM

Thanks for the explanation.
Logged
derekmorgan
Newbie
*
Posts: 3


Email




Reply #3 on: August 08, 2009, 06:00:52 PM

Pascal.

No, autorelease is correct.  It's marked for later release.  If the argument that is passed into setStr: is the same value that is currently stored inside str, then a release would potentially destroy the object.  Alternatively, you could code it in this order:

Code: (Objective-C)
[s retain];
[str release];
str = s;

Cheers,

Steve Kochan


I was just reading this part this evening and was wondering the same thing as Pascal.  I tried passing myStr into setStr: more than once to see what would happen.  With autorelease, it seems that when you pass myStr into setStr: twice, the retain count for myStr is raised to 3, so that after [myA release] and [pool drain], myStr still has a retain count of 1.  With the alternative code that you posted, myStr's retain count does not go above 2.  Is there something that I am missing with the initial method?

Thanks,
Derek
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: August 08, 2009, 07:21:44 PM

I looked again at the code, which is correct.  I did a simple test, like so:

Code: (Objective-C)
int main (int argc, char *argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   NSString *str = [NSMutableString stringWithString: @"a test"];

    NSLog (@"str retain count: %x", [str  retainCount]);
    [pool drain];
  
    NSLog (@"str retain count: %x", [str  retainCount]);
    return 0;
}

Now, str is supposed to be added to the autorelease pool by the stringWithString: method.  But it's not getting released when the pool is drained, as both NSLog calls produce output of 1, indicating that str hasn't been deallocated.   I'll have to do some digging to see if there's something that documents this behavior.

Cheers,

Steve Kochan
Last Edit: August 08, 2009, 07:23:39 PM by skochan Logged
derekmorgan
Newbie
*
Posts: 3


Email




Reply #5 on: August 08, 2009, 08:27:30 PM

I found that if I put another statement, such as NSLog(@"test"), between [pool drain] and NSLog (@"str retain count: %x", [str  retainCount]), it would cause an error that seemed to indicate that str had been deallocated, although I'm not sure why this would make it work.
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #6 on: August 08, 2009, 08:36:19 PM

Okay, that makes sense.  It's probably just a timing issue that's related to the objects getting released while the application is allowed to continue executing.   This is likely the same or similar issue that I noted in another thread about how you're not guaranteed that all your objects will be sent dealloc messages before returning from main (or ending an event loop), as the memory is getting freed anyway.  The point there is not to do any work in a dealloc method that relies on its getting called.

Cheers,

Steve Kochan
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.