现在的位置: 首页 > 综合 > 正文

理解NSString的RetainCount

2013年08月23日 ⁄ 综合 ⁄ 共 2672字 ⁄ 字号 评论关闭

In Xcode, create a command line project with a name NSStringRetainCountTest. And the implementation file is as follows:





#import <Foundation/Foundation.h>

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

    NSString *= [[NSString
alloc]initWithString:@"Hello, string
A."
];

    NSLog(@"Retain count of string a is:%d", [a
retainCount]);

    [pool drain];
    return 0;
}

And the OUTPUT result is:

2009-11-11 16:44:34.890
NSStringRetainCountTest[945:10b] Retain count ofstring a
is:2147483647

    In Cocoa, before MAC 10.5, we cannot use GC(Garbage Collector), so retainCount(Reference Count Mechanism) is a useful memory manager mechanism. Also, even the GC is supported for mac os x, use the retainCount is a good choice to improve ur applications'
performance.

    Maybe u will think the test is strange, when we use alloc/init method to create a object for a class, the retaincount must be 1, but in NSString, we got a very large number: 2147483647, Why?
    The reason is that the @"Hello,
string A." a Constant String, the pointer 'a' points to this string, so, obj-c think:"Aha,
this pointer cannot be release, so give him the retaincount value INT_MAX, but why use INT_MAX? I think this is a suggested value given by the Cocoa developers". SO, when our code is:

    NSString *cc = @"what what what";

    NSString *dd = [NSString stringWithString:@"anything"];

    ...

the result is the same!
    
   But which can release? Let's see the follows' code(we add test sample 2):

    /* Test Sample 2 */
    NSString *= [[NSString
alloc]initWithFormat:@"%s", @"Hello,
string B."
];
    NSLog(@"Retain count of string b is:%d", [b
retainCount]);

   And this sample's OUTPUT result is:

2009-11-11 17:10:56.773
NSStringRetainCountTest[1088:10b] Retain count ofstring b
is:1

  

   The method "initWithFormat:" is a mutable operation, it will copy the strings to a allocated memory. So, the retainCount is 1; It's a little strange, yeah? If we
change the code like this:

    /* Test Sample 2 */

    NSString *= [[NSString
alloc]initWithFormat:@"Aha,%s", @"Hello,
string B."
];

    NSLog(@"Retain count of string b is:%d", [b
retainCount]);


    And now, we can see, @"Hello, string B." is
a constant string, that's right, but it appended to @"Aha,", a new string @"Aha, Hello,
string B." is created, that's to say, a copy of @"Hello,
string B."is created, and this stored into the allocated memory, so the retaincount is 1.

    BUT the Most Strange Thing is this CODE:


    char *cchar = "A1";
    NSString *strss=[[NSString
alloc]initWithUTF8String:cchar];
    NSLog(@"rc of strss is: %d", [strss
retainCount]);
    
    char *cchar2 = "A";//
or = "";

    NSString *strss2=[[NSString
alloc]initWithUTF8String:cchar2];
    NSLog(@"rc of strss is: %d", [strss2
retainCount]);

    The OUTPUT Result is:

2009-11-11 18:16:32.231
NSStringRetainCountTest[2045:10b] rc of strss is: 1
2009-11-11 18:16:32.232
NSStringRetainCountTest[2045:10b] rc of strss is:2147483647

   Who can help me?

抱歉!评论已关闭.