1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/init.cpp
3 // Purpose: initialisation for the library
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/filefn.h"
31 #include "wx/thread.h"
36 #include "wx/ptr_scpd.h"
37 #include "wx/module.h"
38 #include "wx/except.h"
40 #if defined(__WXMSW__) && defined(__WXDEBUG__)
41 #include "wx/msw/msvcrt.h"
43 static struct EnableMemLeakChecking
45 EnableMemLeakChecking()
47 // do check for memory leaks on program exit (another useful flag
48 // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated
49 // memory which may be used to simulate low-memory condition)
50 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
52 } gs_enableLeakChecks
;
53 #endif // __WXMSW__ && __WXDEBUG__
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // we need a dummy app object if the user doesn't want to create a real one
60 class wxDummyConsoleApp
: public wxAppConsole
63 wxDummyConsoleApp() { }
65 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
67 DECLARE_NO_COPY_CLASS(wxDummyConsoleApp
)
70 // we need a special kind of auto pointer to wxApp which not only deletes the
71 // pointer it holds in its dtor but also resets the global application pointer
72 wxDECLARE_SCOPED_PTR(wxAppConsole
, wxAppPtrBase
);
73 wxDEFINE_SCOPED_PTR(wxAppConsole
, wxAppPtrBase
);
75 class wxAppPtr
: public wxAppPtrBase
78 wxEXPLICIT
wxAppPtr(wxAppConsole
*ptr
= NULL
) : wxAppPtrBase(ptr
) { }
83 // the pointer is going to be deleted in the base class dtor, don't
84 // leave the dangling pointer!
85 wxApp::SetInstance(NULL
);
89 void Set(wxAppConsole
*ptr
)
93 wxApp::SetInstance(ptr
);
96 DECLARE_NO_COPY_CLASS(wxAppPtr
)
99 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
101 class wxCallAppCleanup
104 wxCallAppCleanup(wxAppConsole
*app
) : m_app(app
) { }
105 ~wxCallAppCleanup() { if ( m_app
) m_app
->CleanUp(); }
107 void Dismiss() { m_app
= NULL
; }
113 // another tiny class which simply exists to ensure that wxEntryCleanup is
115 class wxCleanupOnExit
118 ~wxCleanupOnExit() { wxEntryCleanup(); }
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 // suppress warnings about unused variables
126 static inline void Use(void *) { }
128 #define WX_SUPPRESS_UNUSED_WARN(x) Use(&x)
130 // ----------------------------------------------------------------------------
131 // initialization data
132 // ----------------------------------------------------------------------------
134 static struct InitData
142 // argv = NULL; -- not even really needed
143 #endif // wxUSE_UNICODE
146 // critical section protecting this struct
147 wxCRIT_SECT_DECLARE_MEMBER(csInit
);
149 // number of times wxInitialize() was called minus the number of times
150 // wxUninitialize() was
156 // if we receive the command line arguments as ASCII and have to convert
157 // them to Unicode ourselves (this is the case under Unix but not Windows,
158 // for example), we remember the converted argv here because we'll have to
159 // free it when doing cleanup to avoid memory leaks
161 #endif // wxUSE_UNICODE
163 DECLARE_NO_COPY_CLASS(InitData
)
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
171 // command line arguments ANSI -> Unicode conversion
172 // ----------------------------------------------------------------------------
176 static void ConvertArgsToUnicode(int argc
, char **argv
)
178 gs_initData
.argv
= new wchar_t *[argc
+ 1];
179 for ( int i
= 0; i
< argc
; i
++ )
181 gs_initData
.argv
[i
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[i
]));
184 gs_initData
.argv
[argc
] = NULL
;
187 static void FreeConvertedArgs()
189 if ( gs_initData
.argv
)
191 for ( int i
= 0; i
< gs_initData
.argc
; i
++ )
193 free(gs_initData
.argv
[i
]);
196 delete [] gs_initData
.argv
;
197 gs_initData
.argv
= NULL
;
201 #endif // wxUSE_UNICODE
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 // initialization which is always done (not customizable) before wxApp creation
208 static bool DoCommonPreInit()
213 // non customizable initialization done after wxApp creation and initialization
214 static bool DoCommonPostInit()
216 wxModule::RegisterModules();
218 return wxModule::InitializeModules();
221 bool wxEntryStart(int& argc
, wxChar
**argv
)
223 // do minimal, always necessary, initialization
224 // --------------------------------------------
227 if ( !DoCommonPreInit() )
233 // first of all, we need an application object
234 // -------------------------------------------
236 // the user might have already created it himself somehow
237 wxAppPtr
app(wxTheApp
);
240 // if not, he might have used IMPLEMENT_APP() to give us a function to
242 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
246 // he did, try to create the custom wxApp object
247 app
.Set((*fnCreate
)());
253 // either IMPLEMENT_APP() was not used at all or it failed -- in any
254 // case we still need something
255 app
.Set(new wxDummyConsoleApp
);
259 // wxApp initialization: this can be customized
260 // --------------------------------------------
262 if ( !app
->Initialize(argc
, argv
) )
267 wxCallAppCleanup
callAppCleanup(app
.get());
269 // for compatibility call the old initialization function too
270 if ( !app
->OnInitGui() )
274 // common initialization after wxTheApp creation
275 // ---------------------------------------------
277 if ( !DoCommonPostInit() )
281 // prevent the smart pointer from destroying its contents
284 // and the cleanup object from doing cleanup
285 callAppCleanup
.Dismiss();
292 // we provide a wxEntryStart() wrapper taking "char *" pointer too
293 bool wxEntryStart(int& argc
, char **argv
)
295 ConvertArgsToUnicode(argc
, argv
);
297 if ( !wxEntryStart(argc
, gs_initData
.argv
) )
307 #endif // wxUSE_UNICODE
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 // cleanup done before destroying wxTheApp
314 static void DoCommonPreCleanup()
317 // flush the logged messages if any and install a 'safer' log target: the
318 // default one (wxLogGui) can't be used after the resources are freed just
319 // below and the user supplied one might be even more unsafe (using any
320 // wxWindows GUI function is unsafe starting from now)
321 wxLog::DontCreateOnDemand();
323 // this will flush the old messages if any
324 delete wxLog::SetActiveTarget(new wxLogStderr
);
327 wxModule::CleanUpModules();
330 // cleanup done after destroying wxTheApp
331 static void DoCommonPostCleanup()
333 wxClassInfo::CleanUp();
335 // we can't do this in wxApp itself because it doesn't know if argv had
339 #endif // wxUSE_UNICODE
341 // check for memory leaks
342 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
343 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
345 wxLogDebug(wxT("There were memory leaks.\n"));
346 wxDebugContext::Dump();
347 wxDebugContext::PrintStatistics();
352 // and now delete the last logger as well
353 delete wxLog::SetActiveTarget(NULL
);
357 void wxEntryCleanup()
359 DoCommonPreCleanup();
362 // delete the application object
368 wxApp::SetInstance(NULL
);
372 DoCommonPostCleanup();
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 #if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
380 #define wxEntryReal wxEntry
381 #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
383 int wxEntryReal(int& argc
, wxChar
**argv
)
385 // library initialization
386 if ( !wxEntryStart(argc
, argv
) )
391 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
392 // below returns or throws
393 wxCleanupOnExit cleanupOnExit
;
395 WX_SUPPRESS_UNUSED_WARN(cleanupOnExit
);
400 // app initialization
401 if ( !wxTheApp
->CallOnInit() )
403 // don't call OnExit() if OnInit() failed
407 // ensure that OnExit() is called if OnInit() had succeeded
411 ~CallOnExit() { wxTheApp
->OnExit(); }
414 WX_SUPPRESS_UNUSED_WARN(callOnExit
);
417 return wxTheApp
->OnRun();
419 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); return -1; )
422 // wrap real wxEntry in a try-except block to be able to call
423 // OnFatalException() if necessary
424 #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
428 #include "wx/msw/private.h"
431 extern unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
);
433 int wxEntry(int& argc
, wxChar
**argv
)
437 return wxEntryReal(argc
, argv
);
439 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
442 ::ExitThread(3); // the same exit code as abort()
444 ::ExitProcess(3); // the same exit code as abort()
447 #if !defined(_MSC_VER) || _MSC_VER < 1300
448 // this code is unreachable but put it here to suppress warnings
449 // from some compilers
455 #endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
459 // as with wxEntryStart, we provide an ANSI wrapper
460 int wxEntry(int& argc
, char **argv
)
462 ConvertArgsToUnicode(argc
, argv
);
464 return wxEntry(argc
, gs_initData
.argv
);
467 #endif // wxUSE_UNICODE
469 // ----------------------------------------------------------------------------
470 // wxInitialize/wxUninitialize
471 // ----------------------------------------------------------------------------
473 bool wxInitialize(int argc
, wxChar
**argv
)
475 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
477 if ( gs_initData
.nInitCount
++ )
479 // already initialized
483 return wxEntryStart(argc
, argv
);
486 void wxUninitialize()
488 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
490 if ( !--gs_initData
.nInitCount
)