Reputation: 35
I've got a problem when I define a UILabel add it to a UIView
UIView *dwView=[[UIView alloc] initWithFrame:CGRectMake(20, 50, 975.0, 620)];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10.0, 160.0, 950.0,
170.0)];
# i add many UILabel in dwView
...
UILabel *label1
UILabel *label2
UILabel *label3
...
dwView.addView(lable1);
dwView.addView(lable2);
dwView.addView(lable3);
dwView.addView(...);
[lable1 release];
[lable2 release];
[lable3 release];
[... release];
No matter where I define the label, I release it with the method:
[lable1 release];
lable1 = nil;
I log the retainCount, its all 0, but I checked the memory with the profile->allocations it's still not reducing.
I want to know why it is like this, and how I can reduce the memory.
edit 1: I built my project with ARC
edit 2:
now I define variable in .h
{
UIView *dwView;
UILabel *label1,lable2;
}
Init in .m
{
dwView=[[[UIView alloc] initWithFrame:CGRectMake(20, 50, 975.0, 620)] autorelease];
label1 = [[[UILabel alloc]initWithFrame:CGRectMake(10.0, 160.0, 950.0, 170.0)] autorelease];
label1.text = wordString;
dwView.addView(lable1);
}
-(void)dealloc {
[super dealloc];
label1 = nil;
dwView = nil;
}
I try the code above, it doesn’t work.
So: how can i release the variable dwView
and lable1
edit
Upvotes: 0
Views: 115
Reputation: 132
It's better to use ARC. Otherwise use autoRelease at the end of your declaration. For example,
UILabel *label1 = [[[UILabel alloc]initWithFrame:CGRectMake(10.0, 160.0,
950.0,170.0)]autorelease];
otherwise release those with
dealloc() label = nil;
Upvotes: -2
Reputation: 7341
Your issue is using Allocations. It doesn't accurately tell you how much live memory you're using. For that you should be using Activity Monitor.
Upvotes: 1
Reputation: 131418
The short answer: Use ARC.
If you're determined to use manual reference counting, a few things:
Looking at retainCount is not useful. It will just confuse you. (You can't see pending autoRelease calls)
In your code, you are creating label1 as a local variable. Then you show ..., meaning you have code somewhere else. If you define a new local variable label1 (and you switch to "lable1" (different spelling) it will be nil. If you want to be able to get to label1 to add it to a superview and/or release it, you either need to do it in the same method/scope, or make it an instance variable.
Another option would be to autoRelease your label after creating it, then add it to the view you want to add it to. That will only work if you add it to your view before the current autoRelease pool is drained.
Upvotes: 3