Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

m4rc0s

macrumors newbie
Original poster
Apr 20, 2009
5
0
Hi *,
I just asked me how it behaves with the retaincount and presenting a view modal. First some code.

I have the following action in my ViewController:
Code:
-(IBAction)showDetailView:(id)sender{
	detailViewController = [[DetailViewController alloc] initWithDetails:(DetailObject *)detailObject];
	[self presentModalViewController:detailViewController animated:NO];
}

In the DetailViewController there is another Action to close the DetailView:
Code:
-(IBAction)closeDetailView:(id)sender{
	[self dismissModalViewControllerAnimated:YES];
}

So far, everything is working fine. I can load a detailed view for several objects and also leave the detailed view and switch back to my main view.

But what about the retainCount of the(se) DetailViewController(s)? Everytime i call the action i allocate a detailview but by closing the detailview i do not release it.

From the Logic i overwrite my variable "detailViewController" on every call. Do I also reserve memory for every call, or is it also overwritten?

Is this a memory Leak? And if, how can I fix it.. :)

Thanks a lot, Marco
 
Typically you release it like this:

Code:
-(IBAction)showDetailView:(id)sender{
	detailViewController = [[DetailViewController alloc] initWithDetails:(DetailObject *)detailObject];
	[self presentModalViewController:detailViewController animated:NO];
        [detailViewController release];
}
 
Typically you release it like this:

Code:
-(IBAction)showDetailView:(id)sender{
	detailViewController = [[DetailViewController alloc] initWithDetails:(DetailObject *)detailObject];
	[self presentModalViewController:detailViewController animated:NO];
        [detailViewController release];
}

Thanks. I´ve tried that. But when I switch back from my detailed view the App ends with Bad Access then.
 
Thanks. I´ve tried that. But when I switch back from my detailed view the App ends with Bad Access then.
I would suspect the issue lies elsewhere then. Better try some debugging to determine what specifically is causing the error. With that kind of info in hand, and relayed to us, we can better help you to troubleshoot.
 
Whenever you create a controller and present it (either using presentModalViewController, or by pushing on to a navigation stack), you can release it straight away as it will be retained by its new owner and released when removed (popped/dismissed).
 
Thanks. I´ve tried that. But when I switch back from my detailed view the App ends with Bad Access then.

The code I showed is the correct way to manage this. If you're getting a crash when the modal is dismissed then it's probably due to an error in the view controller's dealloc method. You need to debug that.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.