iphone 疑问: 内存管理经常碰到的问题

本文探讨了在Objective-C中使用removeFromSuperview时retainCount为何会发生变化的现象,并解释了两个NSLog之间retainCount值突然增加的原因。通过实例分析,揭示了对象生命周期和引用计数机制的微妙之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:问题1:使用 removeFromSuperview   时,retainCount的问题


                NSLog(@"before remove the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

                                 [cameraViewController.view removeFromSuperview];

                NSLog(@"after remove the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

       

 以上代码输出:

          before remove the cameraViewController's retaincount is 2

         before remove the cameraViewController's retaincount is 4

问题疑惑:为什么cameraViewController的retainCount的值会从2变到4呢。 

 

 求解答: 

 

 

 

2:问题1: 两个nslog之间没有任何代码,但是这第一个nslog的%d的值为2,第二个nslog的%d的值为3,太神奇了,不知道为什么?

 

                                             NSLog(@"before remove the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

                                             NSLog(@"after remove the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

 

 3:  下面这段代码的执行结果很让人意外,请看

   

cameraViewController = nil;

cameraViewController = [[IPhoneConnectTestViewController alloc] initWithNibName:@"IPhoneConnectTestViewController" bundle:nil];

                NSLog(@"after alloc the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

[cameraViewController loadCamera: (CameraDO *)obj];

                NSLog(@"after loadCamera the cameraViewController's retaincount is %d",[cameraViewController retainCount]);

                self.title = camera.cameraName;

                [self.view addSubview:cameraViewController.view];

                NSLog(@"after addSubview the cameraViewController's retaincount is %d",[cameraViewController retainCount]); 

 

 

输出结果是:

2011-11-11 16:23:53.065 vsstoo[8755:11f03] after alloc the cameraViewController's retaincount is 1

2011-11-11 16:23:53.066 vsstoo[8755:11f03] after loadCamera the cameraViewController's retaincount is 1

2011-11-11 16:23:53.070 vsstoo[8755:11f03] after addSubview the cameraViewController's retaincount is 3

2011-11-11 16:23:53:250 vsstoo[8755:11f03] socket:0x849bf80 didConnectToHost:121.205.90.83 port:30016

 

[swscaler @ 0x9184c00]No accelerated colorspace conversion found. 

 

 不是道为什么吗?

 

 

转载于:https://www.cnblogs.com/85538649/archive/2011/11/11/2245741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值