开发者

When to release detailsViewController

I have RootViewController and DetailsViewController in my iPhone application. I use Allocations tool to monitor my memory consumption, and I have a question.

When my app starts it takes around 4Mb memory, when I select item in RootViewController it loads UIWebView in DetailsViewController and memory rise up to 10Mb, after I return to RootViewController memory stays at 10Mb lev开发者_StackOverflow社区el and DetailsViewController has retainCount = 2 (even though I create it only once).

How should I free this memory? I know that I should do it if my apps receive memory warning, but I'm creating this ViewController using initWithNibName:, so I understand that I should not send release to it.

Thanks.

Edit

I load it like this:

if (self.detailsViewController == nil)
{        
detailsViewController *d = [[detailsViewController alloc] 
      initWithNibName:@"DetailsViewController" 
      bundle:[NSBundle mainBundle]];

self.detailsViewController = d;
[d release];

self.detailsViewController.urlToLoad = urlToLoad;
}
[self.navigationController pushViewController: self.detailsViewController animated:YES];


Are you using

DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:NIB_NAME bundle:[NSBundle mainbundle]];

Then you must release dvc. Remember the alloc.

Also use the leak took to find out possible leaks. And you should always release the objects that you own when you no longer need them. Not just when you receive memory warnings.


Some code would help with this--otherwise we're just guessing at what you're doing.

Generally speaking, when you add a UIViewController to a UINavigationController, that ViewController is retained, and you should release it. If you add a UIViewController's VIEW as a subview of another view, that VIEW is retained, but not the ViewController.

Bottom line: If you say alloc to ANYTHING, you owe it a release later. That's the rule. Also copy and new, and anything you explicitly retain.

I strongly DON'T recommend tracking retain counts yourself. Things get retained and released behind the scenes for reasons that have very little to do with what's happening in your classes, and you'll see those number shift around in very confusing ways. Best practice is to make sure that YOUR code balances retains and releases. All your brackets have to balance, right? So do all your allocations and releases. It's just that the compiler checks one of those for you, and you're on your own for the other one.


How are you showing your DetailsViewContoller? Through pushViewController:...? If yes, you should release it right after pushing because pushViewController:... retains it.


Try this.         detailsViewController=nil; DetailsViewController *d = [[DetailsViewController alloc]       initWithNibName:@"DetailsViewController"       bundle:[NSBundle mainBundle]];

self.detailsViewController = d;
[d release];

detailsViewController.urlToLoad = urlToLoad;
}
[self.navigationController pushViewController: detailsViewController animated:YES];
[detailsViewController release];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜