+    if ( !DoCommonPostInit() )
+        return false;
+
+
+    // prevent the smart pointer from destroying its contents
+    app.release();
+
+    // and the cleanup object from doing cleanup
+    callAppCleanup.Dismiss();
+
+#if wxUSE_LOG
+    // now that we have a valid wxApp (wxLogGui would have crashed if we used
+    // it before now), we can delete the temporary sink we had created for the
+    // initialization messages -- the next time logging function is called, the
+    // sink will be recreated but this time wxAppTraits will be used
+    delete wxLog::SetActiveTarget(NULL);
+#endif // wxUSE_LOG
+
+    return true;
+}
+
+#if wxUSE_UNICODE
+
+// we provide a wxEntryStart() wrapper taking "char *" pointer too
+bool wxEntryStart(int& argc, char **argv)
+{
+    ConvertArgsToUnicode(argc, argv);
+
+    if ( !wxEntryStart(gs_initData.argc, gs_initData.argv) )
+    {
+        FreeConvertedArgs();
+
+        return false;
+    }
+
+    return true;
+}
+
+#endif // wxUSE_UNICODE
+
+// ----------------------------------------------------------------------------
+// clean up
+// ----------------------------------------------------------------------------
+
+// cleanup done before destroying wxTheApp
+static void DoCommonPreCleanup()
+{
+#if wxUSE_LOG
+    // flush the logged messages if any and don't use the current probably
+    // unsafe log target any more: the default one (wxLogGui) can't be used
+    // after the resources are freed which happens when we return and the user
+    // supplied one might be even more unsafe (using any wxWidgets GUI function
+    // is unsafe starting from now)
+    //
+    // notice that wxLog will still recreate a default log target if any
+    // messages are logged but that one will be safe to use until the very end
+    delete wxLog::SetActiveTarget(NULL);
+#endif // wxUSE_LOG
+}
+
+// cleanup done after destroying wxTheApp
+static void DoCommonPostCleanup()
+{
+    wxModule::CleanUpModules();
+
+    // we can't do this in wxApp itself because it doesn't know if argv had
+    // been allocated
+#if wxUSE_UNICODE
+    FreeConvertedArgs();
+#endif // wxUSE_UNICODE
+
+    // use Set(NULL) and not Get() to avoid creating a message output object on
+    // demand when we just want to delete it
+    delete wxMessageOutput::Set(NULL);
+
+#if wxUSE_LOG
+    // call this first as it has a side effect: in addition to flushing all
+    // logs for this thread, it also flushes everything logged from other
+    // threads
+    wxLog::FlushActive();
+
+    // and now delete the last logger as well
+    //
+    // we still don't disable log target auto-vivification even if any log
+    // objects created now will result in memory leaks because it seems better
+    // to leak memory which doesn't matter much considering the application is
+    // exiting anyhow than to not show messages which could still be logged
+    // from the user code (e.g. static dtors and such)
+    delete wxLog::SetActiveTarget(NULL);
+#endif // wxUSE_LOG
+}
+
+void wxEntryCleanup()
+{
+    DoCommonPreCleanup();
+
+
+    // delete the application object
+    if ( wxTheApp )
+    {
+        wxTheApp->CleanUp();
+
+        // reset the global pointer to it to NULL before destroying it as in
+        // some circumstances this can result in executing the code using
+        // wxTheApp and using half-destroyed object is no good
+        wxAppConsole * const app = wxApp::GetInstance();
+        wxApp::SetInstance(NULL);
+        delete app;
+    }
+
+
+    DoCommonPostCleanup();
+}
+
+// ----------------------------------------------------------------------------
+// wxEntry
+// ----------------------------------------------------------------------------
+
+// for MSW the real wxEntry is defined in msw/main.cpp
+#ifndef __WINDOWS__
+    #define wxEntryReal wxEntry
+#endif // !__WINDOWS__
+
+int wxEntryReal(int& argc, wxChar **argv)
+{
+    // library initialization
+    wxInitializer initializer(argc, argv);
+
+    if ( !initializer.IsOk() )
+    {
+#if wxUSE_LOG
+        // flush any log messages explaining why we failed
+        delete wxLog::SetActiveTarget(NULL);
+#endif
+        return -1;
+    }
+
+    wxTRY
+    {
+        // app initialization
+        if ( !wxTheApp->CallOnInit() )
+        {
+            // don't call OnExit() if OnInit() failed
+            return -1;
+        }
+
+        // ensure that OnExit() is called if OnInit() had succeeded
+        class CallOnExit
+        {
+        public:
+            ~CallOnExit() { wxTheApp->OnExit(); }
+        } callOnExit;
+
+        WX_SUPPRESS_UNUSED_WARN(callOnExit);
+
+        // app execution
+        return wxTheApp->OnRun();
+    }
+    wxCATCH_ALL( wxTheApp->OnUnhandledException(); return -1; )