Kamchatka
Kamchatka

Reputation: 3675

UINavigationController leak/understanding popViewController

I have a navigation controller and a table view. When someone click on the table view, I do the following:

MyViewController *myViewController = [[MyViewController alloc] initWithImage:image];
[image release];
[self.navigationController pushViewController:myViewController animated:YES];
[myViewController release];

myViewController will retain the image.

Now, if I go back and forth in the NavigationController, I get a leak because a new MyViewController gets created each time and apparently the popViewController doesn't release the myViewController.

My question: Why doesn't popViewController release the controller? How should I handle that? Put the myViewController as a member of my class and check if it already exists instead of creating it each time?

Thanks in advance for your help,

Upvotes: 2

Views: 2055

Answers (2)

Kamchatka
Kamchatka

Reputation: 3675

Apparently the problem was that this code was called in another thread and then this thread has to have another autorelease pool to let autorelease work correctly.

Upvotes: 1

Cesar
Cesar

Reputation: 4437

I usually declare the viewController once in the class, alloc in the init and push when needed. Then in the pushed view controller i adjust the view in viewWillAppear:

Upvotes: 0

Related Questions