PDA

View Full Version : any way to ensure the dealloc is called when you terminate?




fenrus110
Dec 2, 2008, 07:00 PM
My app uses a UINavigationController with a few views. In one of my views, I put a small bit of code in my dealloc which basically just saves something to sqlite.

Dealloc isn't necessarily called when you terminate. Though, viewWillDisappear seems to always be called. I guess I could do a little hack to recognize which direction I'm coming from.

Is there a way to guarantee a dealloc when you terminate?



dejo
Dec 3, 2008, 12:49 AM
What about putting it into applicationWillTerminate?

Cromulent
Dec 3, 2008, 02:10 AM
I think the operating system guarantees a dealloc is called on application termination. Otherwise available RAM would fall until nothing was left if you ran applications with small memory leaks until you restarted the phone / machine.

newb16
Dec 3, 2008, 03:30 AM
I think the operating system guarantees a dealloc is called on application termination. Otherwise available RAM would fall until nothing was left if you ran applications with small memory leaks until you restarted the phone / machine.

Os can just free the whole ram allocated to application ( or mark all blocks that were allocated for application by os as free ) and terminate the application. If memory allocated by application leaks when application is terminated, it's the wrong os.

Cromulent
Dec 3, 2008, 03:32 AM
Os can just free the whole ram allocated to application ( or mark all blocks that were allocated for application by os as free ) and terminate the application. If memory allocated by application leaks when application is terminated, it's the wrong os.

There are some operating systems that don't guarantee this :).

mpatric
Dec 3, 2008, 11:06 AM
Dealloc is not called on an object if its reference count is more than 0. If the reference count is 0 it will be called. In other words, if you're retaining more references to an object than you release, dealloc will never get called (and you have a leaky application). The iPhone OS should free up all the memory used by the app when it terminates.

However, having said all that, it's not good practice to do anything other than free up resources and memory in dealloc! You should find a more appropriate place to do this, like applicationWillTerminate, as @dejo suggests.

fenrus110
Dec 3, 2008, 12:14 PM
What about putting it into applicationWillTerminate?

I guess I could do that, but it makes it complicated because I first have to figure out what screen I am on. then figure out the conditions of what I need to do. But AppDelegate does have a reference to the UINavigationController, so I guess you technically access everything.

This also puts in some duplicate code, since I need to the same code as if I were hitting the Back button and in the applicationWillTerminate.

PhoneyDeveloper
Dec 3, 2008, 12:38 PM
Any object can register for the UIApplicationWillTerminateNotification. For a view controller register in init or viewdidload and unregister in dealloc, or wherever makes sense for your class.

Do whatever shutdown processing you want in the callback.

This can be more sensible than using applicationWillTerminate.

dejo
Dec 3, 2008, 12:39 PM
Edit: Ignore this.

fenrus110
Dec 5, 2008, 06:05 PM
Any object can register for the UIApplicationWillTerminateNotification. For a view controller register in init or viewdidload and unregister in dealloc, or wherever makes sense for your class.

Do whatever shutdown processing you want in the callback.

This can be more sensible than using applicationWillTerminate.

I added the applicationWillterminate to the [NSNotificationCenter defaultCenter] to my UIViewController. It works but not in the order I prefer...

the applicationWillTerminate in my AppDelegate gets called first before the one in my UIViewController. Not good, because both of them involve some sqlite interaction. And since my AppDelegate closes the database...

I currently just made a hack where I count the number of viewControllers in the navigationController. If it is a specific number, I leave the database closing duties to that UIViewController class.

PhoneyDeveloper
Dec 5, 2008, 06:22 PM
OK, one more comment on a way to approach this. If you need to have the appDelegate go last you could have your own custom notification. The view controller(s) subscribe to this notification and not the appWillTerminate notification. When your app delegate wants to close the db in appWillTerminate it posts this notification and then closes the db. The notifications are processed synchronously so you're guaranteed that the view controllers will get the chance to update the db before the app delegate closes it. And of course if they don't exist then that's not a problem.