tags:

views:

171

answers:

5

Right now, I do most of my cleanup work in dealloc (cleaning up IBOutlets, allocated objects, etc.). What other places should I do cleanup work in order for my app to be a well-behaved one? Could you explain the things that are typically done in those methods as well?

For example, viewDidUnload, applicationWillResignActive, etc.

A: 

Note: This "Answer" is only relevant to app quit/termination.

According to the answer I received to my question, it's not even necessary at all to do cleanup work like cleaning up IBOutlets, allocated objects, etc. Just save state (as necessary) when your app quits, and let the iPhone OS handle the final cleanup.

Elliot
That's not true because a lot of things that are no longer being used get unloaded long before your app terminates. If you don't do that, your app will use up a lot of memory.
Boon
Ah, good point, thanks. I edited the answer to add a clarification at the top.
Elliot
A: 

Don't forget:

- (void)didReceiveMemoryWarning
rein
A: 

Note that your question is ill-formed. The -dealloc method of UIApplication is never called. The -dealloc of your application's delegate is never called. That means that any objects that are retained by your application's delegate will never be released, so their dealloc is never called.

You should be doing your cleanup in your application delegate's applicationWillTerminate: Since your application is about to die, you don't really need to do anything except give back non-memory resources, make sure your data files are properly closed, and that your NSUserDefaults are synchronized so you can restart properly the next time you are run.

However, any object that might be allocated and deallocated repeatedly over the life of the program deserves a proper Obj-C dealloc method, as documented by Apple, and it is good practice to write this for all your classes, even though they won't be called, just so you build good habits, and readers won't be confused. Also, it saves maintenance headaches in the future, when you DO create and destroy multiple of these, for example in your unit tests.

DavidPhillipOster
+1  A: 

For views, I typically release any UI widgets that were created from the NIB file in viewDidUnload. Any models or other objects I clean up in the viewController's dealloc.

Sometimes I have views that create a model (say a Dictionary of section names to section rows) from a primary data object. If I create/build an object in viewDidLoad I will release it in viewDidUnload (since my viewDidLoad will get called again when the time is right).

I believe that in SDK 3+ you don't have to typically worry about implementing didReceiveMemoryWarning directly as the new viewDidUnload method is the main place to do your view cleanup.

For normal objects (objects without special life cycles like a view controller has) I just release their member vars in the dealloc.

Jason
A: 

I would use the

[yourObject release]
method, but replace yourObject with an object

Mr. Man