X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f7ae43f080576ef142d3884073df298294860328..29a35dd5fee0e33143a39824e71aefd66e3e4851:/src/common/init.cpp?ds=inline diff --git a/src/common/init.cpp b/src/common/init.cpp index 934ba275d2..cf36649317 100644 --- a/src/common/init.cpp +++ b/src/common/init.cpp @@ -34,23 +34,25 @@ #include "wx/init.h" #include "wx/thread.h" -#include "wx/ptr_scpd.h" +#include "wx/scopedptr.h" #include "wx/except.h" -#if defined(__WXMSW__) && defined(__WXDEBUG__) +#if defined(__WXMSW__) #include "wx/msw/msvcrt.h" - static struct EnableMemLeakChecking - { - EnableMemLeakChecking() + #ifdef wxCrtSetDbgFlag + static struct EnableMemLeakChecking { - // do check for memory leaks on program exit (another useful flag - // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated - // memory which may be used to simulate low-memory condition) - wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); - } - } gs_enableLeakChecks; -#endif // __WXMSW__ && __WXDEBUG__ + EnableMemLeakChecking() + { + // check for memory leaks on program exit (another useful flag + // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated + // memory which may be used to simulate low-memory condition) + wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + } + } gs_enableLeakChecks; + #endif // wxCrtSetDbgFlag +#endif // __WXMSW__ // ---------------------------------------------------------------------------- // private classes @@ -62,9 +64,10 @@ class wxDummyConsoleApp : public wxAppConsole public: wxDummyConsoleApp() { } - virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; } + virtual int OnRun() { wxFAIL_MSG( wxT("unreachable code") ); return 0; } + virtual bool DoYield(bool, long) { return true; } - DECLARE_NO_COPY_CLASS(wxDummyConsoleApp) + wxDECLARE_NO_COPY_CLASS(wxDummyConsoleApp); }; // we need a special kind of auto pointer to wxApp which not only deletes the @@ -93,7 +96,7 @@ public: wxApp::SetInstance(ptr); } - DECLARE_NO_COPY_CLASS(wxAppPtr) + wxDECLARE_NO_COPY_CLASS(wxAppPtr); }; // class to ensure that wxAppBase::CleanUp() is called if our Initialize() @@ -110,14 +113,6 @@ private: wxAppConsole *m_app; }; -// another tiny class which simply exists to ensure that wxEntryCleanup is -// always called -class wxCleanupOnExit -{ -public: - ~wxCleanupOnExit() { wxEntryCleanup(); } -}; - // ---------------------------------------------------------------------------- // private functions // ---------------------------------------------------------------------------- @@ -160,7 +155,7 @@ static struct InitData wchar_t **argv; #endif // wxUSE_UNICODE - DECLARE_NO_COPY_CLASS(InitData) + wxDECLARE_NO_COPY_CLASS(InitData); } gs_initData; // ============================================================================ @@ -226,13 +221,6 @@ static bool DoCommonPreInit() #if wxUSE_LOG // Reset logging in case we were cleaned up and are being reinitialized. wxLog::DoCreateOnDemand(); - - // install temporary log sink: we can't use wxLogGui before wxApp is - // constructed and if we use wxLogStderr, all messages during - // initialization simply disappear under Windows - // - // note that we will delete this log target below - delete wxLog::SetActiveTarget(new wxLogBuffer); #endif // wxUSE_LOG return true; @@ -249,44 +237,6 @@ static bool DoCommonPostInit() return false; } -#if defined(__WXDEBUG__) - // check if event classes implement Clone() correctly - // NOTE: the check is done against _all_ event classes which are linked to - // the executable currently running, which are not necessarily all - // wxWidgets event classes. - const wxClassInfo *ci = wxClassInfo::GetFirst(); - for (; ci; ci = ci->GetNext()) - { - // is this class derived from wxEvent? - if (!ci->IsKindOf(CLASSINFO(wxEvent)) || wxString(ci->GetClassName()) == "wxEvent") - continue; - - if (!ci->IsDynamic()) - { - wxLogWarning("The event class '%s' should have a DECLARE_DYNAMIC_CLASS macro!", - ci->GetClassName()); - continue; - } - - // yes; test if it implements Clone() correctly - wxEvent* test = dynamic_cast(ci->CreateObject()); - if (test == NULL) - { - wxLogWarning("The event class '%s' should have a DECLARE_DYNAMIC_CLASS macro!", - ci->GetClassName()); - continue; - } - - wxEvent* cloned = test->Clone(); - if (!cloned || cloned->GetClassInfo() != ci) - wxLogWarning("The event class '%s' does not correctly implement Clone()!", - ci->GetClassName()); - - delete cloned; - delete test; - } -#endif - return true; } @@ -297,9 +247,7 @@ bool wxEntryStart(int& argc, wxChar **argv) // initialize wxRTTI if ( !DoCommonPreInit() ) - { return false; - } // first of all, we need an application object @@ -332,22 +280,15 @@ bool wxEntryStart(int& argc, wxChar **argv) // -------------------------------------------- if ( !app->Initialize(argc, argv) ) - { return false; - } // remember, possibly modified (e.g. due to removal of toolkit-specific // parameters), command line arguments in member variables app->argc = argc; app->argv = argv; - wxCallAppCleanup callAppCleanup(app.get()); - // for compatibility call the old initialization function too - if ( !app->OnInitGui() ) - return false; - // common initialization after wxTheApp creation // --------------------------------------------- @@ -400,14 +341,15 @@ bool wxEntryStart(int& argc, char **argv) static void DoCommonPreCleanup() { #if wxUSE_LOG - // flush the logged messages if any and install a 'safer' log target: the - // default one (wxLogGui) can't be used after the resources are freed just - // below and the user supplied one might be even more unsafe (using any - // wxWidgets GUI function is unsafe starting from now) - wxLog::DontCreateOnDemand(); - - // this will flush the old messages if any - delete wxLog::SetActiveTarget(new wxLogStderr); + // 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 } @@ -428,6 +370,12 @@ static void DoCommonPostCleanup() #if wxUSE_LOG // 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 } @@ -466,7 +414,9 @@ void wxEntryCleanup() int wxEntryReal(int& argc, wxChar **argv) { // library initialization - if ( !wxEntryStart(argc, argv) ) + wxInitializer initializer(argc, argv); + + if ( !initializer.IsOk() ) { #if wxUSE_LOG // flush any log messages explaining why we failed @@ -475,15 +425,8 @@ int wxEntryReal(int& argc, wxChar **argv) return -1; } - // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code - // below returns or throws - wxCleanupOnExit cleanupOnExit; - - WX_SUPPRESS_UNUSED_WARN(cleanupOnExit); - wxTRY { - // app initialization if ( !wxTheApp->CallOnInit() ) { @@ -522,6 +465,11 @@ int wxEntry(int& argc, char **argv) // wxInitialize/wxUninitialize // ---------------------------------------------------------------------------- +bool wxInitialize() +{ + return wxInitialize(0, (wxChar**)NULL); +} + bool wxInitialize(int argc, wxChar **argv) { wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit); @@ -535,6 +483,21 @@ bool wxInitialize(int argc, wxChar **argv) return wxEntryStart(argc, argv); } +#if wxUSE_UNICODE +bool wxInitialize(int argc, char **argv) +{ + wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit); + + if ( gs_initData.nInitCount++ ) + { + // already initialized + return true; + } + + return wxEntryStart(argc, argv); +} +#endif // wxUSE_UNICODE + void wxUninitialize() { wxCRIT_SECT_LOCKER(lockInit, gs_initData.csInit);