OC-7-释放池,指针

这篇博客详细探讨了Objective-C中释放池的工作原理及其与指针的关系。通过输出引用计数的变化,展示了对象在自动释放池中的生命周期和内存管理机制。
摘要由CSDN通过智能技术生成

释放池,指针

//
//  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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值