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"
39 #if defined(__WXMSW__) && defined(__WXDEBUG__)
40 #include "wx/msw/msvcrt.h"
42 static struct EnableMemLeakChecking
44 EnableMemLeakChecking()
46 // do check for memory leaks on program exit (another useful flag
47 // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated
48 // memory which may be used to simulate low-memory condition)
49 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
51 } gs_enableLeakChecks
;
52 #endif // __WXMSW__ && __WXDEBUG__
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // we need a dummy app object if the user doesn't want to create a real one
59 class wxDummyConsoleApp
: public wxAppConsole
62 wxDummyConsoleApp() { }
64 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
66 DECLARE_NO_COPY_CLASS(wxDummyConsoleApp
)
69 // we need a special kind of auto pointer to wxApp which not only deletes the
70 // pointer it holds in its dtor but also resets the global application pointer
71 wxDECLARE_SCOPED_PTR(wxAppConsole
, wxAppPtrBase
);
72 wxDEFINE_SCOPED_PTR(wxAppConsole
, wxAppPtrBase
);
74 class wxAppPtr
: public wxAppPtrBase
77 wxEXPLICIT
wxAppPtr(wxAppConsole
*ptr
= NULL
) : wxAppPtrBase(ptr
) { }
82 // the pointer is going to be deleted in the base class dtor, don't
83 // leave the dangling pointer!
84 wxApp::SetInstance(NULL
);
88 void Set(wxAppConsole
*ptr
)
92 wxApp::SetInstance(ptr
);
95 DECLARE_NO_COPY_CLASS(wxAppPtr
)
98 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
100 class wxCallAppCleanup
103 wxCallAppCleanup(wxAppConsole
*app
) : m_app(app
) { }
104 ~wxCallAppCleanup() { if ( m_app
) m_app
->CleanUp(); }
106 void Dismiss() { m_app
= NULL
; }
112 // another tiny class which simply exists to ensure that wxEntryCleanup is
114 class wxCleanupOnExit
117 ~wxCleanupOnExit() { wxEntryCleanup(); }
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 // suppress warnings about unused variables
125 static inline void Use(void *) { }
127 #define WX_SUPPRESS_UNUSED_WARN(x) Use(&x)
129 // ----------------------------------------------------------------------------
130 // initialization data
131 // ----------------------------------------------------------------------------
133 static struct InitData
141 // argv = NULL; -- not even really needed
142 #endif // wxUSE_UNICODE
145 // critical section protecting this struct
146 wxCRIT_SECT_DECLARE_MEMBER(csInit
);
148 // number of times wxInitialize() was called minus the number of times
149 // wxUninitialize() was
155 // if we receive the command line arguments as ASCII and have to convert
156 // them to Unicode ourselves (this is the case under Unix but not Windows,
157 // for example), we remember the converted argv here because we'll have to
158 // free it when doing cleanup to avoid memory leaks
160 #endif // wxUSE_UNICODE
162 DECLARE_NO_COPY_CLASS(InitData
)
165 // ============================================================================
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // command line arguments ANSI -> Unicode conversion
171 // ----------------------------------------------------------------------------
175 static void ConvertArgsToUnicode(int argc
, char **argv
)
177 gs_initData
.argv
= new wchar_t *[argc
+ 1];
178 for ( int i
= 0; i
< argc
; i
++ )
180 gs_initData
.argv
[i
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[i
]));
183 gs_initData
.argv
[argc
] = NULL
;
186 static void FreeConvertedArgs()
188 if ( gs_initData
.argv
)
190 for ( int i
= 0; i
< gs_initData
.argc
; i
++ )
192 free(gs_initData
.argv
[i
]);
195 delete [] gs_initData
.argv
;
196 gs_initData
.argv
= NULL
;
200 #endif // wxUSE_UNICODE
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 // initialization which is always done (not customizable) before wxApp creation
207 static bool DoCommonPreInit()
212 // non customizable initialization done after wxApp creation and initialization
213 static bool DoCommonPostInit()
215 wxModule::RegisterModules();
217 return wxModule::InitializeModules();
220 bool wxEntryStart(int& argc
, wxChar
**argv
)
222 // do minimal, always necessary, initialization
223 // --------------------------------------------
226 if ( !DoCommonPreInit() )
232 // first of all, we need an application object
233 // -------------------------------------------
235 // the user might have already created it himself somehow
236 wxAppPtr
app(wxTheApp
);
239 // if not, he might have used IMPLEMENT_APP() to give us a function to
241 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
245 // he did, try to create the custom wxApp object
246 app
.Set((*fnCreate
)());
252 // either IMPLEMENT_APP() was not used at all or it failed -- in any
253 // case we still need something
254 app
.Set(new wxDummyConsoleApp
);
258 // wxApp initialization: this can be customized
259 // --------------------------------------------
261 if ( !app
->Initialize(argc
, argv
) )
266 wxCallAppCleanup
callAppCleanup(app
.get());
268 // for compatibility call the old initialization function too
269 if ( !app
->OnInitGui() )
273 // common initialization after wxTheApp creation
274 // ---------------------------------------------
276 if ( !DoCommonPostInit() )
280 // prevent the smart pointer from destroying its contents
283 // and the cleanup object from doing cleanup
284 callAppCleanup
.Dismiss();
291 // we provide a wxEntryStart() wrapper taking "char *" pointer too
292 bool wxEntryStart(int& argc
, char **argv
)
294 ConvertArgsToUnicode(argc
, argv
);
296 if ( !wxEntryStart(argc
, gs_initData
.argv
) )
306 #endif // wxUSE_UNICODE
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 // cleanup done before destroying wxTheApp
313 static void DoCommonPreCleanup()
316 // flush the logged messages if any and install a 'safer' log target: the
317 // default one (wxLogGui) can't be used after the resources are freed just
318 // below and the user supplied one might be even more unsafe (using any
319 // wxWindows GUI function is unsafe starting from now)
320 wxLog::DontCreateOnDemand();
322 // this will flush the old messages if any
323 delete wxLog::SetActiveTarget(new wxLogStderr
);
326 wxModule::CleanUpModules();
329 // cleanup done after destroying wxTheApp
330 static void DoCommonPostCleanup()
332 wxClassInfo::CleanUp();
334 // we can't do this in wxApp itself because it doesn't know if argv had
338 #endif // wxUSE_UNICODE
340 // check for memory leaks
341 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
342 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
344 wxLogDebug(wxT("There were memory leaks.\n"));
345 wxDebugContext::Dump();
346 wxDebugContext::PrintStatistics();
351 // and now delete the last logger as well
352 delete wxLog::SetActiveTarget(NULL
);
356 void wxEntryCleanup()
358 DoCommonPreCleanup();
361 // delete the application object
367 wxApp::SetInstance(NULL
);
371 DoCommonPostCleanup();
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
378 #if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
379 #define wxEntryReal wxEntry
380 #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
382 int wxEntryReal(int& argc
, wxChar
**argv
)
384 // library initialization
385 if ( !wxEntryStart(argc
, argv
) )
390 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
391 // below returns or throws
392 wxCleanupOnExit cleanupOnExit
;
394 WX_SUPPRESS_UNUSED_WARN(cleanupOnExit
);
396 // app initialization
397 if ( !wxTheApp
->CallOnInit() )
399 // don't call OnExit() if OnInit() failed
404 int retValue
= wxTheApp
->OnRun();
412 // wrap real wxEntry in a try-except block to be able to call
413 // OnFatalException() if necessary
414 #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
418 #include "wx/msw/private.h"
421 extern unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
);
423 int wxEntry(int& argc
, wxChar
**argv
)
427 return wxEntryReal(argc
, argv
);
429 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
432 ::ExitThread(3); // the same exit code as abort()
434 ::ExitProcess(3); // the same exit code as abort()
437 #if !defined(_MSC_VER) || _MSC_VER < 1300
438 // this code is unreachable but put it here to suppress warnings
439 // from some compilers
445 #endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
449 // as with wxEntryStart, we provide an ANSI wrapper
450 int wxEntry(int& argc
, char **argv
)
452 ConvertArgsToUnicode(argc
, argv
);
454 return wxEntry(argc
, gs_initData
.argv
);
457 #endif // wxUSE_UNICODE
459 // ----------------------------------------------------------------------------
460 // wxInitialize/wxUninitialize
461 // ----------------------------------------------------------------------------
463 bool wxInitialize(int argc
, wxChar
**argv
)
465 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
467 if ( gs_initData
.nInitCount
++ )
469 // already initialized
473 return wxEntryStart(argc
, argv
);
476 void wxUninitialize()
478 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
480 if ( !--gs_initData
.nInitCount
)