在我的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 (非原子,保留)和@synthesize TabBarController创建的类变量。它接收dealloc方法中的释放命令。
现在,仪器告诉我,在"tabBarController.viewControllers = navigationController“行上有两个内存泄漏。
我一直在折磨自己的头脑,但我不明白为什么:根据我的理解,navigationControllers应该自动释放,如果我在几行之后给它发送一个释放命令,应用程序就会崩溃,所以我猜我是对的。
有没有人能猜到出了什么问题?
非常感谢!
发布于 2010-01-18 19:14:33
首先,您的tabBarController类变量的引用计数增加了两倍。一次来自alloc,一次来自retain,在代码的第一行,但只在dealloc中发布了一次,这可能是内存泄漏的来源。
其次,尽管您已经声明了一个匹配的@property(nonatomic, retain) tabBarController (并通过@sysnthesize实现),但您实际上并没有使用属性访问器(及其在赋值过程中相应的保留和释放行为)来完成此操作,您需要使用self.tabBarController而不仅仅是tabBarController,后者将引用类变量,而不是属性。
尝试将代码修改为以下代码,看看这是否解决了您的问题
// Init tabBar Controller
UITabBarController* tbc = [[[UITabBarController alloc] init];
self.tabBarController = tbc;
[tbc release];
...
- (void)dealloc {
[window release];
self.tabBarController = nil;
[super dealloc]; }https://stackoverflow.com/questions/2078969
复制相似问题