Robert Mao
Robert Mao

Reputation: 1919

Use UINavigationController as modal view, and view are not released after dismiss

I am using a class inherited from UINavigationController present as a modal view, in the navigation bar I have a button 'Done' which will dismiss the modal view when user tap on it. Everything behave normal except the dealloc() in ImagePickerController, GroupPickerController (which is initialized as root view) not get called when I dismiss the modal view. This cause the leak of the memory.

Here is the code use it:

ImagePickerController *picker = [[ImagePickerController alloc] initWithRootViewController:nil];

// don't show animation since this may cause the screen flash with white background. 
[self presentModalViewController:picker animated:NO];

picker.navigationBar.barStyle = UIBarStyleBlack;
[picker release];

Here is what's inside ImagePickerController, which is a UINavigationController:

 - (id)initWithRootViewController:(UIViewController *)root {

    GroupPickerController *controller = [[GroupPickerController alloc] initWithNibName:nil bundle:nil];
    self = [super initWithRootViewController:controller];
    [controller release];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationPageSheet;
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

-(void) dismiss
{
    [self.navigationController setViewControllers:nil];
    [self dismissModalViewControllerAnimated:YES];
}

Here is the code in GroupPickerController, it response to a button in the navigation bar, to dismiss the modal view:

...
#pragma mark - button actions
- (void)done {
    [self.parent dismiss];
}

I tried to manually remove the views from NavigationController, seemed not no effect at all... [self.navigationController setViewControllers:nil];

Thanks for the help!


UPDATED:

Please disregard this question, apparently it's a mistake. :(

Finally get the problem solved... not change any of the code, but a rebuild the project. :(

Upvotes: 0

Views: 971

Answers (2)

hwaxxer
hwaxxer

Reputation: 3383

First of all, you should not be subclassing UINavigationController:

This class is not intended for subclassing.

What does this line do?

controller.parent = self;

If the controller retains the parent-property, you have a retain cycle which would cause the issue you are describing. Remember that all view controllers in the UINavigationController stack can access the navigation controller with the -navigationController property.

Upvotes: 1

teriiehina
teriiehina

Reputation: 4761

There's is a difference between a UIViewController begin dismissed and released. When you dismiss it, it can be released at any moment, but not necessarily immediately.

Are you sure you have a memory leak ? Maybe the picker is released a few seconds after the dimiss.

If you really have a memory leak, that means there is another place where you picker is retained.

Upvotes: 0

Related Questions