Re: retainCount
Re: retainCount
- Subject: Re: retainCount
- From: "John C. Randolph" <email@hidden>
- Date: Fri, 5 Oct 2001 16:24:11 -0700
On Friday, October 5, 2001, at 03:21 PM, Rosyna wrote:
Then I'd have to ask, is it safe to retain an alloc'd instance?
It's safe, but if you're the one who alloc'd it, it's redundant.
NSString* bob=[[NSString alloc] initWithString:@"Bob 0wnz you"];
Bob's retain count is now 1, and no autorelease is pending.
[[bob retain] release];
This line is a NoOp, which bumps the retain count up by one and
immediately reduces it by one again..
NSString* sue=[NSString stringWithString:@"Sue gave Bob a rug-burn."];
Retain count is one, but the string is in the pool and will get a
-release message when the pool does.
[[[[sue retain] autorelease] retain] release];
The net effect of this line, is that sue has a retain count of two, and
*two* pending autoreleases.
Malcolm Crawford and Don Yacktman each did a nice writeup of this
subject on Stepwise. Check them out:
http://www.stepwise.com/Articles/Technical/HoldMe.html
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
-jcr
"I fear all we have done is to awaken a sleeping giant and fill him with
a terrible resolve." -Admiral Isoroku Yamamoto, Dec 7, 1941.