From 8686ca62e79b1d04e0295194c3fea1ba638c7bd9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 29 Jun 2016 18:22:05 +0200 Subject: [PATCH] Avoid asserts when destroying windows with mouse capture in tests Destroying a window with mouse capture results in an assert, which is translated into an exception when running the test suite. As this exception is thrown from wxWindowBase dtor, it results in an immediate program termination when using C++11 and can also have the same effect even when using C++98 if this exception is thrown while already handling another exception due to a test failure. Try to avoid this by using a "safe" DeleteTestWindow() function instead of deleting the window directly. Currently this function ensures that the window doesn't have mouse capture before deleting it, but it could also be used to check for other things later. Also, this commit only uses this function for the two controls which do happen to be destroyed with mouse capture currently (at least when using wxGTK), but it should probably be generalized to all controls in the future. --- tests/controls/listctrltest.cpp | 2 +- tests/html/htmlwindow.cpp | 3 ++- tests/test.cpp | 19 +++++++++++++++++++ tests/testprec.h | 8 ++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/controls/listctrltest.cpp b/tests/controls/listctrltest.cpp index 61098bfdf9..b606b73f44 100644 --- a/tests/controls/listctrltest.cpp +++ b/tests/controls/listctrltest.cpp @@ -82,7 +82,7 @@ void ListCtrlTestCase::setUp() void ListCtrlTestCase::tearDown() { - delete m_list; + DeleteTestWindow(m_list); m_list = NULL; } diff --git a/tests/html/htmlwindow.cpp b/tests/html/htmlwindow.cpp index a1e8befd5e..af9cbb2e33 100644 --- a/tests/html/htmlwindow.cpp +++ b/tests/html/htmlwindow.cpp @@ -78,7 +78,8 @@ void HtmlWindowTestCase::setUp() void HtmlWindowTestCase::tearDown() { - wxDELETE(m_win); + DeleteTestWindow(m_win); + m_win = NULL; } // ---------------------------------------------------------------------------- diff --git a/tests/test.cpp b/tests/test.cpp index 85060d3cd4..87f23166c8 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -513,6 +513,25 @@ static Test *GetTestByName(const wxString& name) return test; } +#if wxUSE_GUI + +void DeleteTestWindow(wxWindow* win) +{ + if ( !win ) + return; + + wxWindow* const capture = wxWindow::GetCapture(); + if ( capture ) + { + if ( capture == win || + capture->GetMainWindowOfCompositeControl() == win ) + capture->ReleaseMouse(); + } + + delete win; +} + +#endif // wxUSE_GUI // ---------------------------------------------------------------------------- // TestApp diff --git a/tests/testprec.h b/tests/testprec.h index 530d5fca35..2c98096250 100644 --- a/tests/testprec.h +++ b/tests/testprec.h @@ -165,6 +165,14 @@ private: wxDECLARE_NO_COPY_CLASS(CLocaleSetter); }; +#if wxUSE_GUI + +// Helper function deleting the window without asserts (and hence exceptions +// thrown from its dtor!) even if it has mouse capture. +void DeleteTestWindow(wxWindow* win); + +#endif // wxUSE_GUI + // Macro that can be used to register the test with the given name in both the // global unnamed registry so that it is ran by default and a registry with the // same name as this test to allow running just this test individually.