Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109842
  • 博文数量: 29
  • 博客积分: 447
  • 博客等级: 下士
  • 技术积分: 414
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-05 23:03
个人简介

整天捣鼓嵌入式,兼职搞搞iOS,这么折腾为了啥?都是为了俺的娃!

文章分类

分类: iOS平台

2015-01-29 22:22:50

下面这个例子是在xcode6.1中编译运行的,已关闭ARC
    
        //NSString *str01 = [[NSString alloc] init];//
这种写法是没任何意义的!!!

        //str01 = @"123456";//指针重新指向了常量字符串,造成前一句的内存泄露

        

        NSString *str02 = [[NSString alloc] initWithString:@"123456"];//编译器会优化成常量字符串,可以释放也可以不释放.retainCount=UINT_MAX

        

        NSString *str03 = [[NSString alloc] initWithFormat:@"123456"];//编译器会优化成常量字符串,可以释放也可以不释放.retainCount=UINT_MAX

        NSString *str04 = [[NSString alloc] initWithFormat:@"123456--%s","hello"];//retainCount=1,必须释放

        

        NSString *str05 = [NSString stringWithFormat:@"123456"];//和str03是同一个字符串,不需要释放(静态方法)

        NSString *str06 = [NSString stringWithFormat:@"123456--%s","hello"];//retainCount=1,不需要释放(静态方法)

        

        NSString *str07 = @"123456";//[NSString stringWithString:@"123456"];//和str02是同一个字符串


        NSLog(@"str01=%p,retainCount=%lx",str01,[str01 retainCount]);

        NSLog(@"str02=%p,retainCount=%lx",str02,[str02 retainCount]);

        NSLog(@"str03=%p,retainCount=%lx",str03,[str03 retainCount]);

        NSLog(@"str04=%p,retainCount=%lx",str04,[str04 retainCount]);

        NSLog(@"str05=%p,retainCount=%lx",str05,[str05 retainCount]);

        NSLog(@"str06=%p,retainCount=%lx",str06,[str06 retainCount]);

        NSLog(@"str07=%p,retainCount=%lx",str07,[str07 retainCount]);

        NSLog(@"----------------------------------------");


        [str01 release];

        [str02 release];

        [str03 release];

        [str04 release];

        

        NSLog(@"str01=%p,retainCount=%lx",str01,[str01 retainCount]);

        NSLog(@"str02=%p,retainCount=%lx",str02,[str02 retainCount]);

        NSLog(@"str03=%p,retainCount=%lx",str03,[str03 retainCount]);

        //NSLog(@"str04=%p,retainCount=%lx",str04,[str04 retainCount]);//retainCount=0,已经被释放了

        NSLog(@"str05=%p,retainCount=%lx",str05,[str05 retainCount]);

        NSLog(@"str06=%p,retainCount=%lx",str06,[str06 retainCount]);//retainCount=1

        NSLog(@"str07=%p,retainCount=%lx",str07,[str07 retainCount]);


运行结果:

2015-01-29 22:06:49.091 Study01-NSString[1309:42746] str01=0x7fff75440bd0,retainCount=ffffffffffffffff

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str02=0x100001030,retainCount=ffffffffffffffff

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str03=0x36353433323165,retainCount=ffffffffffffffff

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str04=0x100406b60,retainCount=1

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str05=0x36353433323165,retainCount=ffffffffffffffff

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str06=0x100406970,retainCount=1

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str07=0x100001030,retainCount=ffffffffffffffff

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] ----------------------------------------

2015-01-29 22:06:49.092 Study01-NSString[1309:42746] str01=0x7fff75440bd0,retainCount=ffffffffffffffff

2015-01-29 22:06:49.093 Study01-NSString[1309:42746] str02=0x100001030,retainCount=ffffffffffffffff

2015-01-29 22:06:49.093 Study01-NSString[1309:42746] str03=0x36353433323165,retainCount=ffffffffffffffff

2015-01-29 22:06:49.093 Study01-NSString[1309:42746] str05=0x36353433323165,retainCount=ffffffffffffffff

2015-01-29 22:06:49.093 Study01-NSString[1309:42746] str06=0x100406970,retainCount=1

2015-01-29 22:06:49.093 Study01-NSString[1309:42746] str07=0x100001030,retainCount=ffffffffffffffff

那么问题来了:
str02和str07的地址是一样的,这很好理解,str02被优化成了常量字符串.
str03和str05的地址是一样的,但是为什么不是和str07的地址一样呢?本质是同样的字符串啊,并且retainCount明明是UINT_MAX。

阅读(1045) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~