title: How to properly close application with exit code
body: <p>In my wxWidgets application I am checking for a command line arguement, and if I find it, I do an action then I close the window. However, I can't seem to get the application to close properly. I want to close the program with an exit code, such as 3. When I check for the command line parameter in wxApp::OnInit, I tried to just call <code>exit(3)</code>, however, this seemed to be improper as it caused memory leaks somewhere within wxwidgets.</p>
  
  <p>I then tried to store the exit code, override OnRun and return there, however, when I do I get a crash in <code>init.cpp</code> line <code>472</code> on <code>return wxTheApp-&gt;OnRun();</code>. </p>
  
  <p>Does anyone know how I can properly close the application with a custom exit code from wxApp after detecting the application should close? I also tried to overload <code>wxApp::OnExit</code>, however, it never ends up being called, even when I don't overload <code>OnRun</code>. </p>
  
  <p>Code example at <a href="http://codepad.org/WYiOJq55" rel="nofollow">http://codepad.org/WYiOJq55</a> due to the code not being allowed in this post for some reason</p>
  
  <p><strong>EDIT</strong> Paste of the code:</p>
  
  <pre><code>int SomeApplication::OnRun()
  {
      if(mExitCode != 0)
      {
          ExitMainLoop();
          return mExitCode;
      }
      else
          return wxApp::OnRun();
  }
  </code></pre>