0

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 level 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];
Burjua
  • 12,506
  • 27
  • 80
  • 111
  • DetailsViewController should have a retainCount of 1 since it's a new object, How are you loading this Nib? – Michael D. Irizarry Aug 26 '10 at 12:01
  • if detailsViewController property is declared as retain (i think which is), then u need to release it, may be in dealloc. – taskinoor Aug 26 '10 at 13:07
  • See my answer below. You should release the controller after pushing it. – tobiasbayer Aug 26 '10 at 13:17
  • Yes, I'm releasing it in dealloc, but it doesn't free memory while RootViewController is alive, I'm I right? – Burjua Aug 26 '10 at 13:24
  • Ok, thanks, I didn't know this, but if I release it after push without setting to nil, retainCount = 1 after return, but app crashes next time I click any item – Burjua Aug 26 '10 at 13:58

4 Answers4

1

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.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
1

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

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
  • This is a more general remark - i've found that XCode's 'Build and Analyze' useful in flagging memory issues in code (be greater \ less than expected retain counts and potential memory leaks). – TanvirK Aug 26 '10 at 12:58
  • Ok, thanks, I didn't know this, but if I release it after push without setting to nil, retainCount = 1 after return, but app crashes next time I click any item – Burjua Aug 26 '10 at 13:23
  • Click any item in the `DetailViewController`? Hard to tell where the problem is without seeing the rest of your code. – tobiasbayer Aug 26 '10 at 13:51
  • If I release DetailViewController after push , then return back to RootViewController from DetailViewController and try to click DetailViewController again I'm getting “EXC_BAD_ACCESS” – Burjua Aug 26 '10 at 14:23
  • How do you return to the `RootViewController`? With `popViewController`? If yes, this releases the popped view controller. Your retain count becomes 0 and the `DetailViewController` is released. You have to allocate a new one before pushing it. – tobiasbayer Aug 26 '10 at 14:43
1

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.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
1

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];
Jordan
  • 21,746
  • 10
  • 51
  • 63
  • yes, I'm doing exactly the same write now, app crashes when I try to do it second time, I solved this problem adding `detailsViewController = nil;` Now I have exact problem as described here: http://stackoverflow.com/questions/2950907/uiwebview-memory-management Is there solution for this? – Burjua Aug 26 '10 at 14:58