理解NSString的RetainCount

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?
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值