views:

43

answers:

2

Hello,

I couldn't realize why this code is being marked as having memory leaks:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    MenuViewController *menuView = [[MenuViewController alloc] initWithNibName:@"MenuView" bundle:[NSBundle mainBundle]];
    navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];
    navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]; // memory leak here 47,1%
    [menuView release];
    BilheteViewController *rightView = [[BilheteViewController alloc] initWithNibName:@"BilheteView" bundle: [NSBundle mainBundle]];
    spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil]; // memory leak here 52,9%
    [window addSubview:spliViewController.view];

    [window makeKeyAndVisible];

    [rightView release];

    return YES;
}

Just the lines marked with problems:

navigationController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"livreto-verso-horizontal.png"]]

and

spliViewController.viewControllers = [NSArray arrayWithObjects:navigationController, rightView, nil];

How do I solve this leaks?

Update 1

App Delegate's dealloc method, both navigationController and spliViewController are being released:

- (void)dealloc {
    [navigationController release];
    [spliViewController release];
    [window release];
    [super dealloc];
}
A: 

Is it possible the only references to the objects you're seeing leaking are in other objects which are also leaking?

For example, you allocate a navigation controller here:

navigationController = [[UINavigationController alloc] initWithRootViewController:menuView];

The UIColor will be retained by that navigationController when you assign it to backgroundColor.

Are you releasing the reference to that navigationController somewhere?

JosephH
Yes, the navigationController is released on the dealloc method of my app delegate (same class where its allocated).
Felipe Cypriano
Is there anywhere else you assign to navigationController? I think you may need to post more code. Are there any other objects leaking?
JosephH
+1  A: 

The leaks tool shows you where something that is leaking, was allocated.

So what it's saying is, after you assigned these values they were never released - which seems like it means your navigation controller is not being released when it should be.

Kendall Helmstetter Gelner
I've updated the question with the dealloc method that's releasing my navigation controller. I don't think it's wrong. Do you?
Felipe Cypriano
Since you are setting up that view in the app delegate that dealloc should never be called.Normally though you would never release a navigation controller in a view controller.
Kendall Helmstetter Gelner
Hi Kendall, so what method should I use to release the controllers on an app delegate? applicationWillFinish?
Felipe Cypriano
dealloc on the application delegate would be the best spot, though really it doesn't matter since your whole application is cleared at that point anyway.
Kendall Helmstetter Gelner