开发者

release viewcontroller after presenting modally

开发者 https://www.devze.com 2022-12-30 05:05 出处:网络
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 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 di开发者_JAVA百科smiss 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?)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消