views:

65

answers:

4

Hi everyone,

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];
+1  A: 

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
yes, I'm doing it
Burjua
+1  A: 

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

tob
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
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
Click any item in the `DetailViewController`? Hard to tell where the problem is without seeing the rest of your code.
tob
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
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.
tob
+1  A: 

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
+1  A: 

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
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