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"
34 #include "wx/ptr_scpd.h"
35 #include "wx/module.h"
37 #if defined(__WXMSW__) && defined(__WXDEBUG__)
38 #include "wx/msw/msvcrt.h"
40 static struct EnableMemLeakChecking
42 EnableMemLeakChecking()
44 // do check for memory leaks on program exit (another useful flag
45 // is _CRTDBG_DELAY_FREE_MEM_DF which doesn't free deallocated
46 // memory which may be used to simulate low-memory condition)
47 wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
);
49 } gs_enableLeakChecks
;
50 #endif // __WXMSW__ && __WXDEBUG__
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // we need a dummy app object if the user doesn't want to create a real one
57 class wxDummyConsoleApp
: public wxAppConsole
60 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
63 // we need a special kind of auto pointer to wxApp which not only deletes the
64 // pointer it holds in its dtor but also resets wxTheApp
65 wxDECLARE_SCOPED_PTR(wxApp
, wxAppPtrBase
);
66 wxDEFINE_SCOPED_PTR(wxApp
, wxAppPtrBase
);
68 class wxAppPtr
: public wxAppPtrBase
71 wxEXPLICIT
wxAppPtr(wxApp
*ptr
= NULL
) : wxAppPtrBase(ptr
) { }
76 // the pointer is going to be deleted in the base class dtor, don't
77 // leave the dangling pointer!
90 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
92 class wxCallAppCleanup
95 wxCallAppCleanup(wxApp
*app
) : m_app(app
) { }
96 ~wxCallAppCleanup() { if ( m_app
) m_app
->CleanUp(); }
98 void Dismiss() { m_app
= NULL
; }
104 // another tiny class which simply exists to ensure that wxEntryCleanup is
106 class wxCleanupOnExit
109 ~wxCleanupOnExit() { wxEntryCleanup(); }
112 // ----------------------------------------------------------------------------
113 // initialization data
114 // ----------------------------------------------------------------------------
116 static struct InitData
124 // argv = NULL; -- not even really needed
125 #endif // wxUSE_UNICODE
128 // critical section protecting this struct
129 wxCRIT_SECT_DECLARE_MEMBER(csInit
);
131 // number of times wxInitialize() was called minus the number of times
132 // wxUninitialize() was
138 // if we receive the command line arguments as ASCII and have to convert
139 // them to Unicode ourselves (this is the case under Unix but not Windows,
140 // for example), we remember the converted argv here because we'll have to
141 // free it when doing cleanup to avoid memory leaks
143 #endif // wxUSE_UNICODE
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // command line arguments ANSI -> Unicode conversion
152 // ----------------------------------------------------------------------------
156 static void ConvertArgsToUnicode(int argc
, char **argv
)
158 gs_initData
.argv
= new wchar_t *[argc
+ 1];
159 for ( int i
= 0; i
< argc
; i
++ )
161 gs_initData
.argv
[i
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[i
]));
164 gs_initData
.argv
[argc
] = NULL
;
167 static void FreeConvertedArgs()
169 for ( int mb_argc
= 0; mb_argc
< wxTheApp
->argc
; mb_argc
++ )
171 free(wxTheApp
->argv
[mb_argc
]);
175 #endif // wxUSE_UNICODE
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 // initialization which is always done (not customizable) before wxApp creation
182 static bool DoCommonPreInit()
184 wxClassInfo::InitializeClasses();
189 // non customizable initialization done after wxApp creation and initialization
190 static bool DoCommonPostInit()
192 wxModule::RegisterModules();
194 return wxModule::InitializeModules();
197 bool wxEntryStart(int& argc
, wxChar
**argv
)
199 // do minimal, always necessary, initialization
200 // --------------------------------------------
203 if ( !DoCommonPreInit() )
209 // first of all, we need an application object
210 // -------------------------------------------
212 // the user might have already created it himself somehow
213 wxAppPtr
app(wxTheApp
);
216 // if not, he might have used IMPLEMENT_APP() to give us a function to
218 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
222 // he did, try to create the custom wxApp object
223 app
.Set((*fnCreate
)());
229 // either IMPLEMENT_APP() was not used at all or it failed -- in any
230 // case we still need something
232 // NB: cast is needed because for the backwards-compatibility reasons
233 // wxTheApp is really a wxApp and not just wxAppConsole...
234 app
.Set((wxApp
*)new wxDummyConsoleApp
);
238 // wxApp initialization: this can be customized
239 // --------------------------------------------
241 if ( !wxTheApp
->Initialize(argc
, argv
) )
246 wxCallAppCleanup
callAppCleanup(wxTheApp
);
248 // for compatibility call the old initialization function too
249 if ( !wxTheApp
->OnInitGui() )
253 // common initialization after wxTheApp creation
254 // ---------------------------------------------
256 if ( !DoCommonPostInit() )
260 // prevent the smart pointer from destroying its contents
263 // and the cleanup object from doing cleanup
264 callAppCleanup
.Dismiss();
271 // we provide a wxEntryStart() wrapper taking "char *" pointer too
272 bool wxEntryStart(int& argc
, char **argv
)
274 ConvertArgsToUnicode(argc
, argv
);
276 if ( !wxEntryStart(argc
, gs_initData
.argv
) )
286 #endif // wxUSE_UNICODE
288 // ----------------------------------------------------------------------------
290 // ----------------------------------------------------------------------------
292 // cleanup done before destroying wxTheApp
293 static void DoCommonPreCleanup()
296 // flush the logged messages if any and install a 'safer' log target: the
297 // default one (wxLogGui) can't be used after the resources are freed just
298 // below and the user supplied one might be even more unsafe (using any
299 // wxWindows GUI function is unsafe starting from now)
300 wxLog::DontCreateOnDemand();
302 // this will flush the old messages if any
303 delete wxLog::SetActiveTarget(new wxLogStderr
);
306 wxModule::CleanUpModules();
309 // cleanup done after destroying wxTheApp
310 static void DoCommonPostCleanup()
312 wxClassInfo::CleanUpClasses();
314 // we can't do this in wxApp itself because it doesn't know if argv had
318 #endif // wxUSE_UNICODE
321 // and now delete the last logger as well
322 delete wxLog::SetActiveTarget(NULL
);
326 void wxEntryCleanup()
328 DoCommonPreCleanup();
331 // delete the application object
341 DoCommonPostCleanup();
343 // check for memory leaks
344 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
345 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
347 wxLogDebug(wxT("There were memory leaks.\n"));
348 wxDebugContext::Dump();
349 wxDebugContext::PrintStatistics();
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 #if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
360 #define wxEntryReal wxEntry
361 #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
363 int wxEntryReal(int& argc
, wxChar
**argv
)
365 // library initialization
366 if ( !wxEntryStart(argc
, argv
) )
371 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
372 // below returns or throws
373 wxCleanupOnExit cleanupOnExit
;
375 // app initialization
376 if ( !wxTheApp
->OnInit() )
378 // don't call OnExit() if OnInit() failed
383 int retValue
= wxTheApp
->OnRun();
385 // why should we do this? it doesn't close all window, just one of them and
386 // this shouldn't be necessary anyhow...
388 // close any remaining windows
389 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
392 // forcibly delete the window.
393 topWindow
->Destroy();
395 // collect the dead objects
396 wxTheApp
->DeletePendingObjects();
406 // wrap real wxEntry in a try-except block to be able to call
407 // OnFatalException() if necessary
408 #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
410 extern unsigned long wxGlobalSEHandler();
412 int wxEntry(int argc
, wxChar
**argv
)
416 return wxEntryReal(argc
, argv
);
418 __except ( wxGlobalSEHandler() )
420 ::ExitProcess(3); // the same exit code as abort()
422 // this code is unreachable but put it here to suppress warnings
427 #endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
431 // as with wxEntryStart, we provide an ANSI wrapper
432 int wxEntry(int argc
, char **argv
)
434 ConvertArgsToUnicode(argc
, argv
);
436 return wxEntry(argc
, gs_initData
.argv
);
439 #endif // wxUSE_UNICODE
441 // ----------------------------------------------------------------------------
442 // wxInitialize/wxUninitialize
443 // ----------------------------------------------------------------------------
445 bool wxInitialize(int argc
, wxChar
**argv
)
447 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
449 if ( gs_initData
.nInitCount
++ )
451 // already initialized
455 return wxEntryStart(argc
, argv
);
458 void wxUninitialize()
460 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
462 if ( !--gs_initData
.nInitCount
)