为什么 Instruments 在 UIColor 和 NSArray 上标记内存泄漏?
我无法意识到为什么这段代码被标记为存在内存泄漏:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:[NSBundle mainBundle]];
navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];
navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]; // memory leak here 47,1%
[menuView release];
BilheteViewController *rightView = [[BilheteViewController alloc] initWithNibName:@"BilheteView" bundle: [NSBundle mainBundle]];
spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil]; // memory leak here 52,9%
[window addSubview:spliViewController.view];
[window makeKeyAndVisible];
[rightView release];
return YES;
}
只是标有问题的行:
navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]
以及
spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil];
如何解决此泄漏?
更新1
App Delegate的dealloc方法,navigationController和spliViewController都被释放:
- (void)dealloc {
[navigationController release];
[spliViewController release];
[window release];
[super dealloc];
}
I couldn't realize why this code is being marked as having memory leaks:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:[NSBundle mainBundle]];
navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];
navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]; // memory leak here 47,1%
[menuView release];
BilheteViewController *rightView = [[BilheteViewController alloc] initWithNibName:@"BilheteView" bundle: [NSBundle mainBundle]];
spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil]; // memory leak here 52,9%
[window addSubview:spliViewController.view];
[window makeKeyAndVisible];
[rightView release];
return YES;
}
Just the lines marked with problems:
navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]
and
spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil];
How do I solve this leaks?
Update 1
App Delegate's dealloc method, both navigationController and spliViewController are being released:
- (void)dealloc {
[navigationController release];
[spliViewController release];
[window release];
[super dealloc];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
泄漏工具会向您显示泄漏的内容的分配位置。
所以它的意思是,在您分配这些值之后,它们从未被释放 - 这似乎意味着您的导航控制器没有在应该被释放的时候被释放。
The leaks tool shows you where something that is leaking, was allocated.
So what it's saying is, after you assigned these values they were never released - which seems like it means your navigation controller is not being released when it should be.
您看到泄漏的对象的唯一引用是否可能在其他也泄漏的对象中?
例如,您在此处分配一个导航控制器:
当您将其分配给backgroundColor时,该导航控制器将保留UIColor。
您是否在某处释放对该 navigationController 的引用?
Is it possible the only references to the objects you're seeing leaking are in other objects which are also leaking?
For example, you allocate a navigation controller here:
The UIColor will be retained by that navigationController when you assign it to backgroundColor.
Are you releasing the reference to that navigationController somewhere?