views:

58

answers:

2

I was watching CS193P Stanford course on Itunes, and in one of the lectures a demo was given and There it was said you could present the viewcontroller modally and then release it. Roughly like this (I know this isn't perfect but I'm on my PC atm)

[self.view presentcontentmodally:myVC]
[myVC release];

However this seems to produce problems. If I put a NSLog(@"%d", [myVC retainCount]) between those two lines then it returns 2 implying it is ok to release. However when I dismiss the myVC the app crashes. Nothing in the NSlog and the debugger won't show where it stopped.

But I used malloc-history or something that some blog said would help. And found that it was the myVC.

So should I be releasing myVC?

(also when the modalVC has been dissmissed should the app's memory usuage go back to before the modalVC was presented?)

A: 

Yes, you should release your view controller after passing it to a modal navigation controller. Just make sure you are not passing in a previously retained view controller unless you plan to manage its release manually.

For example, follow the lifespan of _myViewController here:

MyViewController *_myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
UINavigationController *_modalNavigationController = [[UINavigationController alloc] initWithRootViewController:_myViewController];
[_myViewController release], _myViewController = nil; 
[[self navigationController] presentModalViewController:_modalNavigationController animated:YES];
[_modalNavigationController release], _modalNavigationController = nil;

The modal navigation controller will increment the retain count of _myViewController, essentially taking ownership of it.

Once the modal navigation controller is dismissed and you are back to your original navigation controller, the modal navigation controller will receive a release message and in turn release its root view controller (_myViewController).

The retain count of this view controller will hit zero and the memory is then reclaimed.

Alex Reynolds
A: 

I have just checked through a couple of my apps, and I am releasing my modal view controllers after each presentation, without problems. Which makes me think that you don't yet understand the Cocoa memory management model. Here's a sample:

TweetController *tweetController = [[TweetController alloc] init];
tweetController.content = content;
tweetController.delegate = self;
[self presentModalViewController:tweetController animated:YES];
[tweetController release];

Note that this controller was created with alloc/init, and wasn't previously released or autoreleased.

In addition, please don't rely on retain count checking; a retain could be from a previous autoreleased, which will go away very soon causing the sort of error you have been seeing.

Paul Lynch
That's exactly the same as what I have the controller is created with alloc/init right above where it's added. The debugging told me it was myVC but the app actually crashes when I tap the done button (which sends a message back to the delegate (the first VC) to dismiss it. Strangely it only crashes when done is pressed, not cancel. The difference being 2 strings are passed back to the delegate when done is tapped, where as tapping cancel only dismisses the modalVC.
Jonathan