IOS 4.3 UINavigationBar tintColor Leaks
In IOS4.3 if I set
navigationBar.tintColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
I will get a memory leak: UIDeviceRGBColor leak
But if I use navigationBar.tintColor = [UIColor blackColor];
Everything is fine.
This never happened in ios4.2
I did some debug, and I found the [navigationBar.tintColor retainCount]
seems bigger if I use
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
Does anyone have the same issue?
This is the leak code:
In RootViewController:- (void)viewWillAppear:(BOOL)animated {
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
[super viewWillAppear:animated];
}
In DetailViewController:
- (void)viewWillAppear:(BOOL)animated {
self.navigationController.navigationBar.tintColor = [U开发者_JS百科IColor colorWithRed:0.9 green:0 blue:0 alpha:0];
[super viewWillAppear:animated];
}
If you go to DetailViewController, then popback to RootViewController, in the Instruments, you can see the UIDeviceRGBColor leak
Ive had this issue before 4.2, i think colourWithRed:Green:blue allocates a new UIColor object which your responsible for managing.
The solution is to create an instance for your tint colour and release it when your done with your navigation controller in viewDidUnload.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
tintBarColor = [UIColor
colorWithRed:50.0/255
green:134.0/255
blue:187.0/255
alpha:1];
self.navigationController.navigationBar.tintColor = tintBarColor;
}
- (void)viewDidUnload
{
[super viewDidUnload];
[tintBarColor release];
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
I also see the same problem. I've filed a bug with Apple and I'll post any updates as I hear them.
I have found a workaround though. The problem is in calling self.navigationController.navigationBar.tintColor. But if you set the tint color for a different UIViewController you won't have the same issue. For example, this doesn't seem to leak:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
Unfortunately this doesn't help those of us who use the Three20 framework =(
UPDATE: I heard back from Apple. They said it's already been reported and they are looking into the issue.
First, do not use retainCount
. It is useless.
Next, how do you know you have a leak? Did you use instruments? Finally, did you turn on retain event tracking in the allocations instrument and see where all retain/releases are being sent?
+blackColor
is a singleton. Thus, you are likely leaking it, too, but there is only one and leaks won't find it since it is referenced by a global.
As for your leak, it doesn't matter if it only happens in iOS 4.3 vs. 4.2. A leak is a leak. While there is a chance it is in the Apple frameworks, that isn't likely. However, if so, a bug report is highly appreciated.
Also, use the Allocations instrument to see if you are accreting other objects that are not showing up as leaks (but still shouldn't be around). Leaks only detects unreferencable objects, but there are many other ways to leak memory!
Try using Build and Analyze (or Analyze in Xcode 4) and ensure that you're not leaking elsewhere first.) If you still see the issue, file a bug report with Apple.
I can confirm the same leak in my code - no leak reported in 4.2 and leak appears in 4.3
精彩评论