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 virtual int OnRun() { wxFAIL_MSG( _T("unreachable code") ); return 0; }
65 // we need a special kind of auto pointer to wxApp which not only deletes the
66 // pointer it holds in its dtor but also resets wxTheApp
67 wxDECLARE_SCOPED_PTR(wxApp
, wxAppPtrBase
);
68 wxDEFINE_SCOPED_PTR(wxApp
, wxAppPtrBase
);
70 class wxAppPtr
: public wxAppPtrBase
73 wxEXPLICIT
wxAppPtr(wxApp
*ptr
= NULL
) : wxAppPtrBase(ptr
) { }
78 // the pointer is going to be deleted in the base class dtor, don't
79 // leave the dangling pointer!
92 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
94 class wxCallAppCleanup
97 wxCallAppCleanup(wxApp
*app
) : m_app(app
) { }
98 ~wxCallAppCleanup() { if ( m_app
) m_app
->CleanUp(); }
100 void Dismiss() { m_app
= NULL
; }
106 // another tiny class which simply exists to ensure that wxEntryCleanup is
108 class wxCleanupOnExit
111 ~wxCleanupOnExit() { wxEntryCleanup(); }
114 // ----------------------------------------------------------------------------
115 // initialization data
116 // ----------------------------------------------------------------------------
118 static struct InitData
126 // argv = NULL; -- not even really needed
127 #endif // wxUSE_UNICODE
130 // critical section protecting this struct
131 wxCRIT_SECT_DECLARE_MEMBER(csInit
);
133 // number of times wxInitialize() was called minus the number of times
134 // wxUninitialize() was
140 // if we receive the command line arguments as ASCII and have to convert
141 // them to Unicode ourselves (this is the case under Unix but not Windows,
142 // for example), we remember the converted argv here because we'll have to
143 // free it when doing cleanup to avoid memory leaks
145 #endif // wxUSE_UNICODE
148 // ============================================================================
150 // ============================================================================
152 // ----------------------------------------------------------------------------
153 // command line arguments ANSI -> Unicode conversion
154 // ----------------------------------------------------------------------------
158 static void ConvertArgsToUnicode(int argc
, char **argv
)
160 gs_initData
.argv
= new wchar_t *[argc
+ 1];
161 for ( int i
= 0; i
< argc
; i
++ )
163 gs_initData
.argv
[i
] = wxStrdup(wxConvLocal
.cMB2WX(argv
[i
]));
166 gs_initData
.argv
[argc
] = NULL
;
169 static void FreeConvertedArgs()
171 for ( int mb_argc
= 0; mb_argc
< wxTheApp
->argc
; mb_argc
++ )
173 free(wxTheApp
->argv
[mb_argc
]);
177 #endif // wxUSE_UNICODE
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 // initialization which is always done (not customizable) before wxApp creation
184 static bool DoCommonPreInit()
186 wxClassInfo::InitializeClasses();
191 // non customizable initialization done after wxApp creation and initialization
192 static bool DoCommonPostInit()
194 wxModule::RegisterModules();
196 return wxModule::InitializeModules();
199 bool wxEntryStart(int& argc
, wxChar
**argv
)
201 // do minimal, always necessary, initialization
202 // --------------------------------------------
205 if ( !DoCommonPreInit() )
211 // first of all, we need an application object
212 // -------------------------------------------
214 // the user might have already created it himself somehow
215 wxAppPtr
app(wxTheApp
);
218 // if not, he might have used IMPLEMENT_APP() to give us a function to
220 wxAppInitializerFunction fnCreate
= wxApp::GetInitializerFunction();
224 // he did, try to create the custom wxApp object
225 app
.Set((*fnCreate
)());
231 // either IMPLEMENT_APP() was not used at all or it failed -- in any
232 // case we still need something
234 // NB: cast is needed because for the backwards-compatibility reasons
235 // wxTheApp is really a wxApp and not just wxAppConsole...
236 app
.Set((wxApp
*)new wxDummyConsoleApp
);
240 // wxApp initialization: this can be customized
241 // --------------------------------------------
243 if ( !wxTheApp
->Initialize(argc
, argv
) )
248 wxCallAppCleanup
callAppCleanup(wxTheApp
);
250 // for compatibility call the old initialization function too
251 if ( !wxTheApp
->OnInitGui() )
255 // common initialization after wxTheApp creation
256 // ---------------------------------------------
258 if ( !DoCommonPostInit() )
262 // prevent the smart pointer from destroying its contents
265 // and the cleanup object from doing cleanup
266 callAppCleanup
.Dismiss();
273 // we provide a wxEntryStart() wrapper taking "char *" pointer too
274 bool wxEntryStart(int& argc
, char **argv
)
276 ConvertArgsToUnicode(argc
, argv
);
278 if ( !wxEntryStart(argc
, gs_initData
.argv
) )
288 #endif // wxUSE_UNICODE
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 // cleanup done before destroying wxTheApp
295 static void DoCommonPreCleanup()
298 // flush the logged messages if any and install a 'safer' log target: the
299 // default one (wxLogGui) can't be used after the resources are freed just
300 // below and the user supplied one might be even more unsafe (using any
301 // wxWindows GUI function is unsafe starting from now)
302 wxLog::DontCreateOnDemand();
304 // this will flush the old messages if any
305 delete wxLog::SetActiveTarget(new wxLogStderr
);
308 wxModule::CleanUpModules();
311 // cleanup done after destroying wxTheApp
312 static void DoCommonPostCleanup()
314 wxClassInfo::CleanUpClasses();
316 // we can't do this in wxApp itself because it doesn't know if argv had
320 #endif // wxUSE_UNICODE
323 // and now delete the last logger as well
324 delete wxLog::SetActiveTarget(NULL
);
328 void wxEntryCleanup()
330 DoCommonPreCleanup();
333 // delete the application object
343 DoCommonPostCleanup();
345 // check for memory leaks
346 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
347 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
349 wxLogDebug(wxT("There were memory leaks.\n"));
350 wxDebugContext::Dump();
351 wxDebugContext::PrintStatistics();
357 // ----------------------------------------------------------------------------
359 // ----------------------------------------------------------------------------
361 #if !defined(__WXMSW__) || !wxUSE_ON_FATAL_EXCEPTION
362 #define wxEntryReal wxEntry
363 #endif // !(__WXMSW__ && wxUSE_ON_FATAL_EXCEPTION)
365 int wxEntryReal(int& argc
, wxChar
**argv
)
367 // library initialization
368 if ( !wxEntryStart(argc
, argv
) )
373 // if wxEntryStart succeeded, we must call wxEntryCleanup even if the code
374 // below returns or throws
375 wxCleanupOnExit cleanupOnExit
;
377 // app initialization
378 if ( !wxTheApp
->OnInit() )
380 // don't call OnExit() if OnInit() failed
385 int retValue
= wxTheApp
->OnRun();
387 // why should we do this? it doesn't close all window, just one of them and
388 // this shouldn't be necessary anyhow...
390 // close any remaining windows
391 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
394 // forcibly delete the window.
395 topWindow
->Destroy();
397 // collect the dead objects
398 wxTheApp
->DeletePendingObjects();
408 // wrap real wxEntry in a try-except block to be able to call
409 // OnFatalException() if necessary
410 #if defined(__WXMSW__) && wxUSE_ON_FATAL_EXCEPTION
412 extern unsigned long wxGlobalSEHandler();
414 int wxEntry(int& argc
, wxChar
**argv
)
418 return wxEntryReal(argc
, argv
);
420 __except ( wxGlobalSEHandler() )
422 ::ExitProcess(3); // the same exit code as abort()
424 // this code is unreachable but put it here to suppress warnings
429 #endif // __WXMSW__ && wxUSE_ON_FATAL_EXCEPTION
433 // as with wxEntryStart, we provide an ANSI wrapper
434 int wxEntry(int& argc
, char **argv
)
436 ConvertArgsToUnicode(argc
, argv
);
438 return wxEntry(argc
, gs_initData
.argv
);
441 #endif // wxUSE_UNICODE
443 // ----------------------------------------------------------------------------
444 // wxInitialize/wxUninitialize
445 // ----------------------------------------------------------------------------
447 bool wxInitialize(int argc
, wxChar
**argv
)
449 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
451 if ( gs_initData
.nInitCount
++ )
453 // already initialized
457 return wxEntryStart(argc
, argv
);
460 void wxUninitialize()
462 wxCRIT_SECT_LOCKER(lockInit
, gs_initData
.csInit
);
464 if ( !--gs_initData
.nInitCount
)