Exiting ViewKit ObjectPak Applications

Exiting with the option to abort

If you want to exit a ViewKit ObjectPak application, but also want to allow other parts of the application the option to abort the exiting if necessary, call VkApp::quitYourself():

virtual void quitYourself()

VkApp::quitYourself() calls the okToQuit() function for each top-level VkSimpleWindow (or subclass). All windows that return TRUE are deleted; however, if the okToQuit() function of any window returns FALSE, the shutdown is terminated and the windows returning FALSE are not deleted. quitYourself() queries the windows in the reverse order in which they were created, except that it checks the window designated as the main window last. (See "Managing Top-Level Windows" for information on designating the main window.)

Default behavior

The default, as provided by VkComponent, is for the okToQuit() function to return TRUE in all cases. You must override okToQuit() for all components that you want to perform a check before quitting. For example, you could override the okToQuit() function for a window to post a dialog asking the user whether he or she really wants to exit the application and then abort the shutdown if the user says to do so. Another possibility would be to return FALSE if a component is in the process of updating a file.

Checking components before exiting

Usually, only VkSimpleWindow and its subclasses use okToQuit(). In some cases, you might want to check one or more components contained within a window before quitting. To do so, override the okToQuit() function for that window to call the okToQuit() functions for all the desired components. Override the okToQuit() functions for the other components to perform whatever checks are necessary.

Exiting Automatically

A ViewKit ObjectPak application automatically exits when all of its windows are deleted, under any of the following circumstances:

  • Application calls quitYourself()
  • Application deletes all of its windows individually
  • User deletes all application windows through window manager interaction (for example, selecting the "Close" option in the window menu provided by the window manager)


Once all windows are deleted, the application exits by calling VkApp::terminate():

virtual void terminate(int status = 0)

terminate() is a virtual function that calls exit(2). terminate() is also called from within ObjectPak when any fatal error is detected.

Calling terminate()

You can call terminate() explicitly to exit a ViewKit ObjectPak application immediately. Usually you would use this if you encounter a fatal error. If you provide a status argument, your application uses it as the exit value that the application returns.

Overriding terminate()

You can override terminate() in a VkApp subclass to perform any cleanup operations that your application requires before aborting (for example, closing a database). If you override terminate() in a derived class, call the base class's terminate() function after performing your cleanup operations.

Note: Although you can override quitYourself() in a VkApp subclass, in most cases you should override terminate() instead. This ensures that any cleanup operations you add are performed no matter how the application exits (for example, by error condition or by user interaction with the window manager). If you decide to override quitYourself(), you must perform your cleanup operations before calling the base class's quitYourself(): if quitYourself() succeeds in deleting all windows, your application calls terminate() and exits before ever returning from quitYourself().