可能重复:
Reference count is still 1 after [obj release], when it should be deallocated
1.我写这段代码时。
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
[self.view addSubview:label];
label.text =@"label Text";
[label release];
[label release];
NSLog(@"LableRetainCount = %i \n",lable.retainCount);
输出:LableRetainCount 1.Lable保留计数不会从1减少为什么?
2.我写这段代码。
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
[self.view addSubview:lable];
label.text =@"lable Text";
label = nil;
NSLog(@"LabelRetainCount = %i \n",label.retainCount);
输出:LabelRetainCount = 0当我设置“label = nil”时,它的保留计数变为0为什么? 它是meanes内存释放这个对象?
答案 0 :(得分:1)
如果你想正确地释放它,就像#1一样,首先你要在你的.text行之后这样做:
[label removeFromSuperview]; // remove the view that you added, view will release it
[label release];
然后你的保留计数应为0,对象将被释放。