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

Discussion in 'iOS Programming' started by fenrus110, Dec 2, 2008.

  1. macrumors regular

    Joined:
    Mar 24, 2008
    #1
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What about putting it into applicationWillTerminate?
     
  3. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    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

    Joined:
    Feb 27, 2008
    #4
    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

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    There are some operating systems that don't guarantee this :).
     
  6. macrumors newbie

    Joined:
    Oct 20, 2008
    #6
    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. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #7
    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

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  10. thread starter macrumors regular

    Joined:
    Mar 24, 2008
    #10
    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

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    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