views:

304

answers:

1

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 alltogether 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 = navigatioControllers". 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!

+1  A: 

Firstly, your tabBarController class variable has it's reference count increased twice. Once from the alloc and once from the retain in the first line of your code, yet is only released once in dealloc 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 use self.tabBarController rather than just tabBarController which will refer to the class variable, not the property.

Try modifying your code to the following to see if this solves your problem

// Init tabBar Controller
UITabBarController* tbc = [[[UITabBarController alloc] init];
self.tabBarController = tbc;
[tbc release];
...

- (void)dealloc {
[window release];
self.tabBarController = nil;
[super dealloc]; }
cidered
Thanks for your solution. I have changed the code in the way you proposed, yet I was not able to get it to work. I also do not quite understand the property vs. class variable thing: What are the main differences? So far I have always used @property and @synthesize to declare class variables.I use the exact same code as you suggested, yet I still get the leak on the line where the Nav-/view Controllers are added. If I try to release them a few lines later, the app crashes (which also kind of makes sense as they should be available afterwards). Any ideas on that? Thanks a lot for you help!
Robin
Rather than explain it all here I suggest you read as mush as possible of Apple's Objective C programming reference http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf. The relevant sections to my answer are the chapter on 'Declared Properties' and the discussion of the use of self when accessing properties under 'Object Messaging'
cidered