Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
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?
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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

macrumors regular
Feb 27, 2008
100
0
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

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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

macrumors newbie
Oct 20, 2008
21
0
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

macrumors regular
Original poster
Mar 24, 2008
142
0
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

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

fenrus110

macrumors regular
Original poster
Mar 24, 2008
142
0
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

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.