views:

323

answers:

1

I would expect that after I push a view controller I then need to release my ownership of the view controller like I did below.

CustomViewController *nextViewController = [[CustomViewController alloc] initWithNibName:@"CustomView" bundle:nil];
[[self navigationController] pushViewController:nextViewController animated:YES];
[nextViewController release];

After I do that, I assume that the navigation controller has ownership of that object and will release it when done which would then call dealloc on my customViewController. I would expect that to happen when I tap the back button on the navigation bar and the view is no longer displayed. That does not happen though. I added an NSLog(@"CustomViewController did receive dealloc") into the dealloc method of CustomViewController but it never gets printed. Is this normal behavior?

Is the navigation controller just doing something like keeping that object in case it needs it at some point? Will it get rid of it when memory starts to run out? I tried simulating a low memory warning but nothing happens. I have a feeling the answer to this question will be that I should just not worry so much and follow the standard procedure for retain/release/autorelease. That said though, has any one else delved into this a little bit further and found out an absolute answer?

+4  A: 

"Is the navigation controller just doing something like keeping that object in case it needs it at some point?"

Yup. That is exactly what it is doing. It will send you viewDidLoad and viewDidUnload messages though. And that is the place where you should allocate and free resources that your viewcontroller uses.

Note that viewDidUnload will only be called when the system thinks it needs to free memory.

If you need more immediate control over when things are loaded and freed then a better place might be viewWillAppear: and viewDidDisappear:.

My alloc/init methods for viewcontrollers are usually pretty empty or not even there.

St3fan
According to documentation, viewDidUnload is only sent in cases where your device has entered low-memory conditions. There isn't any indications that viewDidUnload is involved with deallocation.
Giao
You are right of course. Updated my answer :-)
St3fan
That brings up more ?1) What is the dealloc method inside my viewconroller? 2) Is the navigation controller just waiting till it get some sort of signal to finally get rid of old view controllers?3) If yes to #2 then what signal? I put an nslog message into my did recieve memory warning method in my view controller class and after pushing and popping several times then simulating a memory warning i get N messages on the console. That tells me all the objects are still there and the memory warning did not cause them to get deallocated as the log message in the dealloc message does not print.
startoftext
Sorry, typo, I meant to say. What is the purpose of the dealloc method inside my view controller if other methods will be used for freeing resources?
startoftext
Ah, I finally figured out what the deal was. I had a collection of model objects that have the view controller as their delegate. What happened is in those model objects I had set the @property to retain. Sorry if this was kinda confusing but thank you every one for your time.
startoftext