X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a83f860948059b0273b5cc6d9e43fadad3ebfca..2d5efafe74f3788bfd3b3d8b1e8e13135a595ed5:/samples/except/except.cpp diff --git a/samples/except/except.cpp b/samples/except/except.cpp index ed04aec596..1cb291eeb5 100644 --- a/samples/except/except.cpp +++ b/samples/except/except.cpp @@ -44,6 +44,8 @@ #include "wx/utils.h" #include "wx/msgdlg.h" #include "wx/icon.h" + + #include "wx/thread.h" #endif // ---------------------------------------------------------------------------- @@ -51,7 +53,7 @@ // ---------------------------------------------------------------------------- // the application icon (under Windows and OS/2 it is in resources) -#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "../sample.xpm" #endif @@ -91,14 +93,13 @@ public: // crash (e.g. dereferencing null pointer, division by 0, ...) virtual void OnFatalException(); -#ifdef __WXDEBUG__ - // in debug mode, you can override this function to do something different - // (e.g. log the assert to file) whenever an assertion fails - virtual void OnAssert(const wxChar *file, - int line, - const wxChar *cond, - const wxChar *msg); -#endif // __WXDEBUG__ + // you can override this function to do something different (e.g. log the + // assert to file) whenever an assertion fails + virtual void OnAssertFailure(const wxChar *file, + int line, + const wxChar *func, + const wxChar *cond, + const wxChar *msg); }; // Define a new frame type: this is going to be our main frame @@ -119,6 +120,7 @@ public: void OnThrowUnhandled(wxCommandEvent& event); void OnCrash(wxCommandEvent& event); + void OnTrap(wxCommandEvent& event); #if wxUSE_ON_FATAL_EXCEPTION void OnHandleCrash(wxCommandEvent& event); #endif @@ -129,10 +131,13 @@ protected: // catch exceptions which occur in MyFrame methods here virtual bool ProcessEvent(wxEvent& event); -#ifdef __WXDEBUG__ - // show how an assert failure message box looks like + // provoke assert in main or worker thread + // + // this is used to show how an assert failure message box looks like void OnShowAssert(wxCommandEvent& event); -#endif // __WXDEBUG__ +#if wxUSE_THREADS + void OnShowAssertInThread(wxCommandEvent& event); +#endif // wxUSE_THREADS private: // any class wishing to process wxWidgets events must use this macro @@ -184,12 +189,14 @@ enum Except_ThrowObject, Except_ThrowUnhandled, Except_Crash, + Except_Trap, #if wxUSE_ON_FATAL_EXCEPTION Except_HandleCrash, #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ Except_ShowAssert, -#endif // __WXDEBUG__ +#if wxUSE_THREADS + Except_ShowAssertInThread, +#endif // wxUSE_THREADS Except_Dialog, Except_Quit = wxID_EXIT, @@ -212,12 +219,14 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(Except_ThrowObject, MyFrame::OnThrowObject) EVT_MENU(Except_ThrowUnhandled, MyFrame::OnThrowUnhandled) EVT_MENU(Except_Crash, MyFrame::OnCrash) + EVT_MENU(Except_Trap, MyFrame::OnTrap) #if wxUSE_ON_FATAL_EXCEPTION EVT_MENU(Except_HandleCrash, MyFrame::OnHandleCrash) #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ EVT_MENU(Except_ShowAssert, MyFrame::OnShowAssert) -#endif // __WXDEBUG__ +#if wxUSE_THREADS + EVT_MENU(Except_ShowAssertInThread, MyFrame::OnShowAssertInThread) +#endif // wxUSE_THREADS END_EVENT_TABLE() BEGIN_EVENT_TABLE(MyDialog, wxDialog) @@ -302,19 +311,28 @@ void MyApp::OnFatalException() wxT("wxExcept Sample"), wxOK | wxICON_ERROR); } -#ifdef __WXDEBUG__ - -void MyApp::OnAssert(const wxChar *file, - int line, - const wxChar *cond, - const wxChar *msg) +void MyApp::OnAssertFailure(const wxChar *file, + int line, + const wxChar *func, + const wxChar *cond, + const wxChar *msg) { - // we don't have anything special to do here - wxApp::OnAssert(file, line, cond, msg); + // take care to not show the message box from a worker thread, this doesn't + // work as it doesn't have any event loop + if ( !wxIsMainThread() || + wxMessageBox + ( + wxString::Format("An assert failed in %s().", func) + + "\n" + "Do you want to call the default assert handler?", + "wxExcept Sample", + wxYES_NO | wxICON_QUESTION + ) == wxYES ) + { + wxApp::OnAssertFailure(file, line, func, cond, msg); + } } -#endif // __WXDEBUG__ - // ============================================================================ // MyFrame implementation // ============================================================================ @@ -338,19 +356,23 @@ MyFrame::MyFrame() menuFile->Append(Except_ThrowUnhandled, wxT("Throw &unhandled exception\tCtrl-U")); menuFile->Append(Except_Crash, wxT("&Crash\tCtrl-C")); + menuFile->Append(Except_Trap, "&Trap\tCtrl-T", + "Break into the debugger (if one is running)"); menuFile->AppendSeparator(); #if wxUSE_ON_FATAL_EXCEPTION menuFile->AppendCheckItem(Except_HandleCrash, wxT("&Handle crashes\tCtrl-H")); menuFile->AppendSeparator(); #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ menuFile->Append(Except_ShowAssert, wxT("Provoke &assert failure\tCtrl-A")); +#if wxUSE_THREADS + menuFile->Append(Except_ShowAssertInThread, + wxT("Assert failure in &thread\tShift-Ctrl-A")); +#endif // wxUSE_THREADS menuFile->AppendSeparator(); -#endif // __WXDEBUG__ menuFile->Append(Except_Quit, wxT("E&xit\tCtrl-Q"), wxT("Quit this program")); wxMenu *helpMenu = new wxMenu; - helpMenu->Append(Except_About, wxT("&About...\tF1"), wxT("Show about dialog")); + helpMenu->Append(Except_About, wxT("&About\tF1"), wxT("Show about dialog")); // now append the freshly created menu to the menu bar... wxMenuBar *menuBar = new wxMenuBar(); @@ -430,6 +452,11 @@ void MyFrame::OnCrash(wxCommandEvent& WXUNUSED(event)) DoCrash(); } +void MyFrame::OnTrap(wxCommandEvent& WXUNUSED(event)) +{ + wxTrap(); +} + #if wxUSE_ON_FATAL_EXCEPTION void MyFrame::OnHandleCrash(wxCommandEvent& event) @@ -439,8 +466,6 @@ void MyFrame::OnHandleCrash(wxCommandEvent& event) #endif // wxUSE_ON_FATAL_EXCEPTION -#ifdef __WXDEBUG__ - void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) { // provoke an assert from wxArrayString @@ -448,7 +473,34 @@ void MyFrame::OnShowAssert(wxCommandEvent& WXUNUSED(event)) arr[0]; } -#endif // __WXDEBUG__ +#if wxUSE_THREADS + +void MyFrame::OnShowAssertInThread(wxCommandEvent& WXUNUSED(event)) +{ + class AssertThread : public wxThread + { + public: + AssertThread() + : wxThread(wxTHREAD_JOINABLE) + { + } + + protected: + virtual void *Entry() + { + wxFAIL_MSG("Test assert in another thread."); + + return 0; + } + }; + + AssertThread thread; + thread.Create(); + thread.Run(); + thread.Wait(); +} + +#endif // wxUSE_THREADS void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) {