Re: retainCount
Re: retainCount
- Subject: Re: retainCount
- From: "Erik M. Buck" <email@hidden>
- Date: Fri, 5 Oct 2001 17:52:38 -0500
>
Then I'd have to ask, is it safe to retain an alloc'd instance?
>
>
NSString* bob=[[NSString alloc] initWithString:@"Bob 0wnz you"];
>
[[bob retain] release];
>
NSString* sue=[NSString stringWithString:@"Sue gave Bob a rug-burn."];
>
[[[[sue retain] autorelease] retain] release];
>
Yes, of course it safe to retain an alloc'd instance.
In the code you provide, bob will never be dealloced.
bob is alloced : bob retain count = 1
bob is retained: bob retain count = 2
bob is released: bob retain count = 1
sue is created with convenience method: sue retain count = 1, but sue is
already in autorelease pool once
sue is retained: sue retain count = 2, and sue is in autorelease pool once
sue is autoreleased: sue retain count still 2, and sue is in autorelease
pool twice
sue is retained: sue reatin count = 3, and sue is in autorelease pool twice
sue is released: sue retain count = 2, and sue is in autorelease pool twice
sue will be correctly released twice and deallocated when the autorelease
pool is deallocated.