tabBarController 内存泄漏
在我的 AppDelegate 中,我启动了一个 tabBar 控制器,其中添加了一堆 navigationController 作为选项卡。我使用以下代码:
// Init tabBar Controller
tabBarController = [[[UITabBarController alloc] init] retain];
// Init Root Views of navigation controllers
FirstRootViewController* firstViewController = [[[FirstRootViewController alloc] init] autorelease];
SecondRootViewController* secondViewController = [[[SecondRootViewController alloc] init] autorelease];
ThirdRootViewController* thirdViewController = [[[ThirdRootViewController alloc] init] autorelease];
// Init Navigation controllers of tabs
UINavigationController* firstNavController = [[[UINavigationController alloc] initWithRootViewController:firstViewController] autorelease];
UINavigationController* secondNavController = [[[UINavigationController alloc] initWithRootViewController:secondViewController] autorelease];
UINavigationController* thirdNavController = [[[UINavigationController alloc] initWithRootViewController:thirdViewController] autorelease];
firstNavController.navigationBar.barStyle = UIBarStyleBlack;
secondNavController.navigationBar.barStyle = UIBarStyleBlack;
thirdNavController.navigationBar.barStyle = UIBarStyleBlack;
// Create array for tabBarController and add navigation controllers to tabBarController
NSArray *navigationControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdNavController, nil];
tabBarController.viewControllers = navigationControllers;
[window addSubview:tabBarController.view];
和 dealloc 函数:
- (void)dealloc {
[window release];
[tabBarController release];
[super dealloc]; }
firstNavController 是要添加的导航控制器,几行后将它们一起正确释放(它们是使用 alloc 创建的)。
TabBarController 是一个使用@property(非原子,retain)和@synthesize tabBarController 创建的类变量。它在 dealloc 方法中接收释放命令。
现在,仪器告诉我,“tabBarController.viewControllers = navigationController”线上有两个内存泄漏。
我已经折磨了我的头,但我不明白为什么:根据我的理解,导航控制器应该自动释放,如果我在几行后向它发送释放命令,应用程序就会崩溃,所以我想我是对的。
任何猜测出了什么问题吗?
多谢!
In my AppDelegate I initiate a tabBar Controller, to which a bunch of navigationController is added as tabs. I use the following code:
// Init tabBar Controller
tabBarController = [[[UITabBarController alloc] init] retain];
// Init Root Views of navigation controllers
FirstRootViewController* firstViewController = [[[FirstRootViewController alloc] init] autorelease];
SecondRootViewController* secondViewController = [[[SecondRootViewController alloc] init] autorelease];
ThirdRootViewController* thirdViewController = [[[ThirdRootViewController alloc] init] autorelease];
// Init Navigation controllers of tabs
UINavigationController* firstNavController = [[[UINavigationController alloc] initWithRootViewController:firstViewController] autorelease];
UINavigationController* secondNavController = [[[UINavigationController alloc] initWithRootViewController:secondViewController] autorelease];
UINavigationController* thirdNavController = [[[UINavigationController alloc] initWithRootViewController:thirdViewController] autorelease];
firstNavController.navigationBar.barStyle = UIBarStyleBlack;
secondNavController.navigationBar.barStyle = UIBarStyleBlack;
thirdNavController.navigationBar.barStyle = UIBarStyleBlack;
// Create array for tabBarController and add navigation controllers to tabBarController
NSArray *navigationControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, thirdNavController, nil];
tabBarController.viewControllers = navigationControllers;
[window addSubview:tabBarController.view];
And the dealloc function:
- (void)dealloc {
[window release];
[tabBarController release];
[super dealloc]; }
firstNavController are the navigation controllers to be added which are properly released altogether a few lines later (they are created using alloc).
TabBarController is a class variable which has been created using @property (nonatomic, retain) and @synthesize tabBarController. It receives a release command in the dealloc method.
Now instruments tells me that I have two memory leaks on the line with "tabBarController.viewControllers = navigationController".
I have tortured my head, yet I don't see why: From my understanding, navigationControllers should get released automatically and if I send it a release command a few lines later, the app crashes, so I guess I am right.
Any guesses whats wrong?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的
tabBarController
类变量的引用计数增加了两倍。一次来自代码第一行的alloc
和一次来自retain
,但仅在dealloc
中释放一次,这可能是您的位置内存泄漏是从哪里来的。其次,虽然您已经声明了一个匹配的 @property(nonatomic, keep) tabBarController (并通过 @sysnthesize 实现),但您实际上并没有使用属性访问器(及其相应的保留) & 赋值期间的释放行为)为此,您需要使用
self.tabBarController
而不仅仅是tabBarController
,后者将引用类变量,而不是属性。尝试将您的代码修改为以下内容,看看是否可以解决您的问题
Firstly, your
tabBarController
class variable has it's reference count increased twice. Once from thealloc
and once from theretain
in the first line of your code, yet is only released once indealloc
This is probably where your memory leak is coming from.Secondly, although you have declared a matching
@property(nonatomic, retain) tabBarController
(and implemented via@sysnthesize
) you are not actually using the property accessors (and its corresponding retain & release behaviour during assignment) To do this you need to useself.tabBarController
rather than justtabBarController
which will refer to the class variable, not the property.Try modifying your code to the following to see if this solves your problem