views:

322

answers:

2

Hello,

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,

A: 

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:

Cesar
+1  A: 

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.

Kamchatka