1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

any way to ensure the dealloc is called when you terminate?

Discussion in 'iPhone/iPad Programming' started by fenrus110, Dec 2, 2008.

  1. macrumors regular

    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?
  2. Moderator


    Staff Member

    What about putting it into applicationWillTerminate?
  3. macrumors 603


    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.
  4. macrumors regular

    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.
  5. macrumors 603


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

    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.
  7. macrumors regular

    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.
  8. macrumors 68030


    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.
  9. Moderator


    Staff Member

    Edit: Ignore this.
  10. macrumors regular

    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.
  11. macrumors 68030


    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.

Share This Page