X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9482617091b084d4ee006679a450176715d465e0..8531cc2589161b92c1d8b607b0c13e4d1c8b6844:/src/common/init.cpp diff --git a/src/common/init.cpp b/src/common/init.cpp index e696eacb12..fe406b7be9 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -31,6 +31,8 @@ #include "wx/thread.h" #endif +#include "wx/init.h" + #include "wx/ptr_scpd.h" #include "wx/module.h" @@ -87,6 +89,20 @@ public: } }; +// class to ensure that wxAppBase::CleanUp() is called if our Initialize() +// fails +class wxCallAppCleanup +{ +public: + wxCallAppCleanup(wxApp *app) : m_app(app) { } + ~wxCallAppCleanup() { if ( m_app ) m_app->CleanUp(); } + + void Dismiss() { m_app = NULL; } + +private: + wxApp *m_app; +}; + // another tiny class which simply exists to ensure that wxEntryCleanup is // always called class wxCleanupOnExit @@ -95,6 +111,15 @@ public: ~wxCleanupOnExit() { wxEntryCleanup(); } }; +// ---------------------------------------------------------------------------- +// private functions +// ---------------------------------------------------------------------------- + +// suppress warnings about unused variables +static inline void Use(void *) { } + +#define WX_SUPPRESS_UNUSED_WARN(x) Use(&x) + // ---------------------------------------------------------------------------- // initialization data // ---------------------------------------------------------------------------- @@ -125,7 +150,7 @@ static struct InitData // them to Unicode ourselves (this is the case under Unix but not Windows, // for example), we remember the converted argv here because we'll have to // free it when doing cleanup to avoid memory leaks - wchar_t *argv; + wchar_t **argv; #endif // wxUSE_UNICODE } gs_initData; @@ -152,9 +177,15 @@ static void ConvertArgsToUnicode(int argc, char **argv) static void FreeConvertedArgs() { - for ( int mb_argc = 0; mb_argc < wxTheApp->argc; mb_argc++ ) + if ( gs_initData.argv ) { - free(wxTheApp->argv[mb_argc]); + for ( int i = 0; i < gs_initData.argc; i++ ) + { + free(gs_initData.argv[i]); + } + + delete [] gs_initData.argv; + gs_initData.argv = NULL; } } @@ -180,7 +211,7 @@ static bool DoCommonPostInit() return wxModule::InitializeModules(); } -bool wxEntryStart(int argc, wxChar **argv) +bool wxEntryStart(int& argc, wxChar **argv) { // do minimal, always necessary, initialization // -------------------------------------------- @@ -229,26 +260,33 @@ bool wxEntryStart(int argc, wxChar **argv) return false; } + wxCallAppCleanup callAppCleanup(wxTheApp); + + // for compatibility call the old initialization function too + if ( !wxTheApp->OnInitGui() ) + return false; + // common initialization after wxTheApp creation // --------------------------------------------- if ( !DoCommonPostInit() ) - { return false; - } // prevent the smart pointer from destroying its contents app.release(); + // and the cleanup object from doing cleanup + callAppCleanup.Dismiss(); + return true; } #if wxUSE_UNICODE // we provide a wxEntryStart() wrapper taking "char *" pointer too -bool wxEntryStart(int argc, char **argv) +bool wxEntryStart(int& argc, char **argv) { ConvertArgsToUnicode(argc, argv); @@ -339,7 +377,7 @@ void wxEntryCleanup() #define wxEntryReal wxEntry #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION) -int wxEntryReal(int argc, wxChar **argv) +int wxEntryReal(int& argc, wxChar **argv) { // library initialization if ( !wxEntryStart(argc, argv) ) @@ -351,8 +389,10 @@ int wxEntryReal(int argc, wxChar **argv) // below returns or throws wxCleanupOnExit cleanupOnExit; + WX_SUPPRESS_UNUSED_WARN(cleanupOnExit); + // app initialization - if ( !wxTheApp->OnInit() ) + if ( !wxTheApp->CallOnInit() ) { // don't call OnExit() if OnInit() failed return -1; @@ -386,9 +426,14 @@ int wxEntryReal(int argc, wxChar **argv) // OnFatalException() if necessary #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION +#ifdef __WXWINCE__ +// For ExitThread +#include "wx/msw/private.h" +#endif + extern unsigned long wxGlobalSEHandler(); -int wxEntry(int argc, wxChar **argv) +int wxEntry(int& argc, wxChar **argv) { __try { @@ -396,10 +441,17 @@ int wxEntry(int argc, wxChar **argv) } __except ( wxGlobalSEHandler() ) { +#ifdef __WXWINCE__ + ::ExitThread(3); // the same exit code as abort() +#else ::ExitProcess(3); // the same exit code as abort() +#endif +#if !defined(_MSC_VER) || _MSC_VER < 1300 // this code is unreachable but put it here to suppress warnings + // from some compilers return -1; +#endif } } @@ -408,7 +460,7 @@ int wxEntry(int argc, wxChar **argv) #if wxUSE_UNICODE // as with wxEntryStart, we provide an ANSI wrapper -int wxEntry(int argc, char **argv) +int wxEntry(int& argc, char **argv) { ConvertArgsToUnicode(argc, argv);