首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tabBarController内存泄漏

tabBarController内存泄漏
EN

Stack Overflow用户
提问于 2010-01-17 05:31:05
回答 1查看 1.8K关注 0票数 0

在我的AppDelegate中,我启动了一个tabBar控制器,其中添加了一组navigationController作为选项卡。我使用以下代码:

代码语言:javascript
复制
// 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函数:

代码语言:javascript
复制
- (void)dealloc {
[window release];
[tabBarController release];
[super dealloc]; }

firstNavController是要添加的导航控制器,它们会在几行之后正确发布(它们是使用alloc创建的)。

tabBarController是一个使用@property (非原子,保留)和@synthesize TabBarController创建的类变量。它接收dealloc方法中的释放命令。

现在,仪器告诉我,在"tabBarController.viewControllers = navigationController“行上有两个内存泄漏。

我一直在折磨自己的头脑,但我不明白为什么:根据我的理解,navigationControllers应该自动释放,如果我在几行之后给它发送一个释放命令,应用程序就会崩溃,所以我猜我是对的。

有没有人能猜到出了什么问题?

非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2010-01-18 19:14:33

首先,您的tabBarController类变量的引用计数增加了两倍。一次来自alloc,一次来自retain,在代码的第一行,但只在dealloc中发布了一次,这可能是内存泄漏的来源。

其次,尽管您已经声明了一个匹配的@property(nonatomic, retain) tabBarController (并通过@sysnthesize实现),但您实际上并没有使用属性访问器(及其在赋值过程中相应的保留和释放行为)来完成此操作,您需要使用self.tabBarController而不仅仅是tabBarController,后者将引用类变量,而不是属性。

尝试将代码修改为以下代码,看看这是否解决了您的问题

代码语言:javascript
复制
// Init tabBar Controller
UITabBarController* tbc = [[[UITabBarController alloc] init];
self.tabBarController = tbc;
[tbc release];
...

- (void)dealloc {
[window release];
self.tabBarController = nil;
[super dealloc]; }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2078969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档