释放池,指针
// // main.m // test // // Created by myhaspl on 16/11/4. // Copyright (c) 2016年 myhaspl@myhaspl.com. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { //@autoreleasepool { // insert code here... //手动释放 NSNumber *mynum=[[NSNumber alloc]initWithDouble:3.1415926]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); NSNumber * mynum1=mynum; [mynum retain]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); NSNumber * mynum2=mynum; [mynum retain]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); [mynum retain]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); [mynum release]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); [mynum release]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); [mynum release]; NSLog(@"引用计数为:%lu",(unsigned long)[mynum retainCount]); //自动释放池1 //@autoreleasepool { // Objective-C的Auto Reference Counting为YES // } NSLog(@"自动释放池2"); //自动释放池2 Objective-C的Auto Reference Counting为NO //@autoreleasepool { NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];//创建池 NSNumber *yournum=[[NSNumber alloc]initWithDouble:3.1415926]; NSLog(@"引用计数为:%lu",(unsigned long)[yournum retainCount]); [yournum autorelease]; [yournum autorelease]; [yournum retain]; [yournum retain]; NSLog(@"引用计数为:%lu",(unsigned long)[yournum retainCount]); [pool drain]; NSLog(@"引用计数为:%lu",(unsigned long)[yournum retainCount]); return 0; }
2016-11-04 16:40:41.411 test[710:22710] 引用计数为:1
2016-11-04 16:40:41.412 test[710:22710] 引用计数为:2
2016-11-04 16:40:41.412 test[710:22710] 引用计数为:3
2016-11-04 16:40:41.413 test[710:22710] 引用计数为:4
2016-11-04 16:40:41.413 test[710:22710] 引用计数为:3
2016-11-04 16:40:41.413 test[710:22710] 引用计数为:2
2016-11-04 16:40:41.413 test[710:22710] 引用计数为:1
2016-11-04 16:40:41.413 test[710:22710] 自动释放池2
2016-11-04 16:40:41.413 test[710:22710] 引用计数为:1
2016-11-04 16:40:41.414 test[710:22710] 引用计数为:3
2016-11-04 16:40:41.414 test[710:22710] 引用计数为:1