views:

101

answers:

4

Aren't these methods called when the app is about to be shut down? If so, then won't the memory be all cleared out anyway?

A: 

Keep in mind that each class inerithing from NSObject has its dealloc and so when the reference count of an object reaches 0 , its dealloc is being called, meaning that the memory owned by that object would better be deallocated.

Similarly viewDidUnload is a method that each UIViewController has and it is being called when the main view associated to the controller is no more needed, no more visible if you want (you can think of it being called when you a pop the controller from a navigation stack or switch a tab in tabbar controller). It is convenient for the app and the iPhone/iPod not to have the objects owned by the view around when the view is not displayed/active/used etc.

Finally the AppDelegate, as an object has its own dealloc method, so maybe your confusion can come from this point.

rano
here go the random downvoters ^^ at least say why
rano
Your answer assumes that I'm all confused about things that I'm not. It addresses those things, and doesn't really answer my question well.
awakeFromNib
Maybe you shall downvote your question since it is bad written ^^ Plus, you'd better clarify with someone who posts in a comment and work on your acceptance rate
rano
My question is clear and concise, i.e. well written. I guess English is not your native language.
awakeFromNib
You are right, English is not my native language. Sill, you seemed not able to formulate the question that fits your needs, looking at the answers we all gave you (pretty much the same) and looking at your past questions
rano
No I asked the question I was thinking about. It is because English is not your native language that you do not understand.
awakeFromNib
+1  A: 

Aren't these methods called when the app is about to be shut down? If so, then won't the memory be all cleared out anyway?

It is true that viewDidUnload and dealloc are called when an app terminates, but these are certainly not the only times. It is very important to correctly implement these cleanup methods, as well as didReceiveMemoryWarning.

If you don't clean up properly in dealloc, then your app will start to leak memory. Over time, it may consume more and more memory, until it gets terminated by the system.

Similarly, if your viewDidUnload doesn't release its resources, you can be leaking memory. If the view is used multiple times, each invocation will leak.

Careful memory management is more important than ever with iOS 4, as your application may end up in the background if the user presses the Home button. This means it may run for longer than ever, and thus you will be reusing the same view controllers when it regains the foreground. If your app doesn't release unused memory properly, it will almost certainly be killed by the system.

gavinb
If the app terminates, then all memory leaks are gone, right?
awakeFromNib
Yes, when the app terminates, the OS will release all resources. But relying on this to do all the cleanup for anything but a trivial app is dangerous (as hotpaw2 explains).
gavinb
+1  A: 

If you only have one view that lasts the duration of the app, then unload and dealloc are currently never even called, so these methods are actually unused and unneeded.

However, if you ever expand this app to have views and objects that get switched in and out of use, then in low memory circumstances these methods may well be called to lower your app's memory footprint so that the app doesn't get killed for using too much memory. So leaving them in (and coding them correctly to release internally allocated objects and malloc'd memory) for future code reuse is considered good practice. That's why they come with the various Cocoa templates.

hotpaw2
+1  A: 

viewDidUnload is only called in low memory situations. You want to release all object you create in viewDidLoad. You want to pair them up. You still want to release everything in dealloc, since viewDidUnload will not be called if low memory situations never occur in your app.

bstahlhood