Application shutdown

The application normally shuts down when the last of its top level windows is closed. This is normally the expected behaviour and means that it is enough to call Close() in response to the "Exit" menu command if your program has a single top level window. If this behaviour is not desirable wxApp::SetExitOnFrameDelete can be called to change it. Note that starting from wxWidgets 2.3.3 such logic doesn't apply for the windows shown before the program enters the main loop: in other words, you can safely show a dialog from wxApp::OnInit and not be afraid that your application terminates when this dialog - which is the last top level window for the moment - is closed.

Another aspect of the application shutdown is OnExit which is called when the application exits but before wxWidgets cleans up its internal structures. You should delete all wxWidgets object that you created by the time OnExit finishes. In particular, do not destroy them from application class' destructor!

For example, this code may crash:

class MyApp : public wxApp
{
 public:
    wxCHMHelpController m_helpCtrl;
    ...
};

The reason for that is that m_helpCtrl is a member object and is thus destroyed from MyApp destructor. But MyApp object is deleted after wxWidgets structures that wxCHMHelpController depends on were uninitialized! The solution is to destroy HelpCtrl in OnExit:

class MyApp : public wxApp
{
 public:
    wxCHMHelpController *m_helpCtrl;
    ...
};

bool MyApp::OnInit()
{
  ...
  m_helpCtrl = new wxCHMHelpController;
  ...
}

int MyApp::OnExit()
{
  delete m_helpCtrl;
  return 0;
}
ymasuda 平成17年11月19日