views:

1181

answers:

2

I have a query about the use of viewDidUnload and dealloc in a simple implementation of UITableViewController.

Essentially I'd like to know more about weather or not both viewDidUnload and dealloc are always called in succession in the tear down process. Is it possible that dealloc could be called on the controller without viewDidUnload ?

In either case, releasing the same iVars and retained references in both functions wouldn't cause any problems - but I was wondering if anyone knew for sure or could shed some insight into the tear down process.

Cheers,

A: 

viewDidUnload is called when the view controller's view is released. In the context you're referring to, dealloc is called on the view controller. It seems then that they are always called in that order, viewDidUnload then dealloc, unless viewDidUnload is not called at all which I suppose could happen if there is no view associated with the controller which I'm not even sure is possible without crashing. So it seems that the likely answer to your question is that they both always get called and always in that order.

This all sounds hypothetical. What are you trying to figure out? In other words, why does it matter?

The Apple docs have a brief explanation of what is going on in -viewDidUnload

Update: I was completely wrong on this one. Sorry about that. See Manjunath's answer.

Matt Long
Thanks Matt, my queries are just for my own better understanding of how Apple's built in classes operate. It looks more and more like viewDidUnload is called and the viewController's view is then cleaned up so anything else left over should be released in dealloc.
Jessedc
I've just checked and viewDidUnload is not called when application terminates.
Piotr Czapla
+12  A: 

Hi Jessedec,

What Matt has told is not right!!! viewDidUnload will not be called every time like dealloc method. viewDidUnload is called only when your app receives low memory warning! Just think, If you are releasing your object both in viewDidUnload and dealloc methods. If both gets called every time, then you are releasing already released object, which will lead to application crash isn't it?. ViewDidLoad is a place provided by the Apple for cleaning up the things when receiving the low memory warning because you know in iphone we have memory restriction.

Manjunath
I think you mean `viewDidUnload` when you say `viewDidLoad` in your answer. `viewDidLoad` is not called when there's low memory, but when the view is going to appear onscreen (before `viewWillAppear`). `viewDidUnload` is called when there's a low memory warning.
nevan
This should have been the correct answer. This got me in trouble today.
asandroq