IOS7内存释放问题 [英] IOS7 memory release issue
问题描述
我的IOS程序不是ARC,代码如下:
My IOS program is not ARC, code like this:
在 .h 文件中我定义了五个变量:
in the .h file i define five variables:
{
UILabel *label1,*lable2;
UIView *dwView;
NSMutableArray *wordsArray;
}
.m 文件中的代码如下:
the code in the .m file like this:
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
viewArray=[[NSMutableArray alloc]init];
}
-(void)QuestionA{
dwView=[[UIView alloc] initWithFrame:CGRectMake(20, 50, 975.0, 620)];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 160.0, 950.0, 170.0)];
label2 = [[UILabel alloc]initWithFrame:CGRectMake(30.0, 160.0, 950.0, 170.0)];
[dwView addSubview:label1];
[dwView addSubview:label2];
[self.view addSubview:dwView];
[viewArray addObject:dwView];
[lable1 release];
[lable2 release];
[dwView release];
}
在我转向另一个活动之前,我注销了这些变量的保留计数:
before I turn to another activity I log out the retain count of those variables:
{
[lable1 retainCount] ---> 2
[lable2 retainCount] ---> 2
[dwView retainCount] ---> 3
}
所以:我想知道为什么会这样,以及如何释放保留计数到 0?
So: I wonder why it like this, and how can I release the retain count to 0?
推荐答案
大概这些是在例程结束时调用release
"之前的保留计数.
Presumably these are the retain counts before you call "release
" at the end of your routine.
调用传统的 "alloc
"/"init
" 启动新实例化的对象,保留计数为 1.当您将 dwView 作为子视图添加到父视图时视图,这会增加保留计数.当您将 dwView 添加到数组时,也会增加保留计数.因此有 3 个.
Calling the traditional "alloc
" / "init
" starts the newly instantiated object off with a retain count of 1. When you add dwView as a subview to a parent view, that increments the retain count. When you add dwView to the array, that also increments the retain count. Hence 3 there.
对于标签也是如此,您已将它们添加为子视图,因此每个标签的保留计数增加 1(保留计数为 2).
Same for the labels, you've added them as subviews, so that increments the retain count by 1 for each (giving you a retain count of 2).
当保留计数达到零时,对象将被释放(例如,对于您的dwView",这将是
self.view"被释放时以及"
viewArray`" 被释放).
The object will be released when the retain count hits zero (e.g. for your "dwView", that'll be when "
self.view" is dealloc'd and when the "
viewArray`" gets released).
这篇关于IOS7内存释放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!