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] initWithRoot开发者_如何学PythonViewController: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!
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]; }
精彩评论