Re: NSString initWithUTF8String - crazy retainCount with empty string
Re: NSString initWithUTF8String - crazy retainCount with empty string
- Subject: Re: NSString initWithUTF8String - crazy retainCount with empty string
- From: Andy Lee <email@hidden>
- Date: Sat, 24 Mar 2007 12:23:29 -0400
On Mar 24, 2007, at 11:33 AM, Simon Strandgaard wrote:
I have asserts on retainCount a few places in my code,
and today I got surprised that with NSString sets a weird
retainCount when initialized with an empty string.
It looks like a bug to me?
It is not a bug. I just did a quick test and found that the
initWithXXX and stringWithXXX methods treat empty strings as a
special case, and always return the same instance. I ran the
following code:
NSLog(@"----- empty initWithXXX -----");
char *fred = "fred";
char *barney = "barney";
NSLog(@"0x%x", [[[NSString alloc] initWithUTF8String:""]
autorelease]);
NSLog(@"0x%x", [[[NSString alloc] initWithUTF8String:""]
autorelease]);
NSLog(@"0x%x", [[[NSString alloc] initWithBytes:fred length:0
encoding:NSUTF8StringEncoding] autorelease]);
NSLog(@"0x%x", [[[NSString alloc] initWithBytes:barney length:0
encoding:NSUTF8StringEncoding] autorelease]);
NSLog(@"0x%x", [[[NSString alloc] initWithData:[NSData data]
encoding:NSUTF8StringEncoding] autorelease]);
NSLog(@"----- empty stringWithXXX -----");
NSLog(@"0x%x", [[NSString stringWithUTF8String:""] autorelease]);
NSLog(@"----- empty NSString literals -----");
NSLog(@"0x%x", [@"" autorelease]);
NSLog(@"0x%x", [@"" autorelease]);
NSLog(@"----- non-empty but equal initWithXXX -----");
NSLog(@"0x%x", [[[NSString alloc] initWithUTF8String:"abc"]
autorelease]);
NSLog(@"0x%x", [[[NSString alloc] initWithUTF8String:"abc"]
autorelease]);
...and got the following output:
2007-03-24 12:13:22.168 Scratch[14401] ----- empty initWithXXX -----
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] ----- empty stringWithXXX -----
2007-03-24 12:13:22.169 Scratch[14401] 0xa080c988
2007-03-24 12:13:22.169 Scratch[14401] ----- empty NSString literals
-----
2007-03-24 12:13:22.169 Scratch[14401] 0x50f8
2007-03-24 12:13:22.169 Scratch[14401] 0x50f8
2007-03-24 12:13:22.169 Scratch[14401] ----- non-empty but equal
initWithXXX -----
2007-03-24 12:13:22.169 Scratch[14401] 0x360860
2007-03-24 12:13:22.169 Scratch[14401] 0x364500
This explains the constantly increasing retain count -- it is a
retain count on a behind-the-scenes global object that is never meant
to be deallocated. (Interestingly, that global object is not the
same as @"".)
The above is one example why, as mmalc says, you shouldn't concern
yourself with retain counts. If you want to check for memory leaks,
there are tools to help detect them. It might also work in *your
own* classes to log retains and releases and make sure they are
balanced.
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden