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 // Note: check for memory leaks is now done via wxDebugContextDumpDelayCounter
343 // and now delete the last logger as well
344 delete wxLog::SetActiveTarget(NULL
);
348 void wxEntryCleanup()
350 DoCommonPreCleanup();
353 // delete the application object
359 wxApp::SetInstance(NULL
);
363 DoCommonPostCleanup();
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 #if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
371 #define wxEntryReal wxEntry
372 #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
374 int wxEntryReal(int& argc
, wxChar
**argv
)
376 // library initialization
377 if ( !wxEntryStart(argc
, argv
) )
382 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
383 // below returns or throws
384 wxCleanupOnExit cleanupOnExit
;
386 WX_SUPPRESS_UNUSED_WARN(cleanupOnExit
);
391 // app initialization
392 if ( !wxTheApp
->CallOnInit() )
394 // don't call OnExit() if OnInit() failed
398 // ensure that OnExit() is called if OnInit() had succeeded
402 ~CallOnExit() { wxTheApp
->OnExit(); }
405 WX_SUPPRESS_UNUSED_WARN(callOnExit
);
408 return wxTheApp
->OnRun();
410 wxCATCH_ALL( wxTheApp
->OnUnhandledException(); return -1; )
413 // wrap real wxEntry in a try-except block to be able to call
414 // OnFatalException() if necessary
415 #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
419 #include "wx/msw/private.h"
422 extern unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
);
424 int wxEntry(int& argc
, wxChar
**argv
)
428 return wxEntryReal(argc
, argv
);
430 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
433 ::ExitThread(3); // the same exit code as abort()
435 ::ExitProcess(3); // the same exit code as abort()
438 #if !defined(_MSC_VER) || _MSC_VER < 1300
439 // this code is unreachable but put it here to suppress warnings
440 // from some compilers
446 #endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
450 // as with wxEntryStart, we provide an ANSI wrapper
451 int wxEntry(int& argc
, char **argv
)
453 ConvertArgsToUnicode(argc
, argv
);
455 return wxEntry(argc
, gs_initData
.argv
);
458 #endif // wxUSE_UNICODE
460 // ----------------------------------------------------------------------------
461 // wxInitialize/wxUninitialize
462 // ----------------------------------------------------------------------------
464 bool wxInitialize(int argc
, wxChar
**argv
)
466 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
468 if ( gs_initData
.nInitCount
++ )
470 // already initialized
474 return wxEntryStart(argc
, argv
);
477 void wxUninitialize()
479 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
481 if ( !--gs_initData
.nInitCount
)