Reputation: 1371
I have the following code which adds a label into a footer of a UITableView, so that I can format the text (white, etc.)
It works ok, but it gives me a leak warning for the "headerLabel" when analyzing it on the line with the "return"
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;
if (section==0) {
headerLabel.text = @"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element
}
[customView addSubview:headerLabel];
//[headerLabel release];
return customView;
// [customView release];
I've tried to put the release here and there, but it's always the same.
I'd appreciate some feedback from you guys.
Upvotes: 0
Views: 353
Reputation: 17445
autorelease your customView and make sure you are releasing headerLabel after you add it as a subview. Anytime you call alloc/init you are taking ownership, you need to make sure you release those objects. Since you are returning customView from this method it makes sense to defer your release of that object (using autorelease) so it can be used by the calling object.
// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc]
initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)]
autorelease];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;
if (section==0) {
headerLabel.text = @"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element
}
[customView addSubview:headerLabel];
[headerLabel release];
return customView;
Upvotes: 1
Reputation: 135588
You have to release headerLabel
before exiting the method:
[headerView release];
You probably should autorelease customView
unless your method name includes the words new
, alloc
or copy
(in that case, the caller would have to release the returned view):
return [customView autorelease];
Upvotes: 1
Reputation: 52237
try
[headerLabel release];
return [customView autorelease];
Upvotes: 1
Reputation: 5827
Based on the code sample you've got here, the first release would be correct. (Releasing after the return statement wouldn't make sense). You took ownership of the object when you created it, and you need to release it.
You can use Instruments to track where object is being retained and released; you can see a history of the leaky object to see exactly what's going on. That would be your best bet here. Launch your app with the Leaks instrument, and when you find the leaking object, click the arrow to the right of the address. This will show you the object's history - every retain and release.
Upvotes: 0