diff --git a/include/wx/list.h b/include/wx/list.h index c39f305aca..7a72519a35 100644 --- a/include/wx/list.h +++ b/include/wx/list.h @@ -1292,18 +1292,17 @@ public: #endif // wxLIST_COMPATIBILITY // delete all list elements -// -// NB: the class declaration of the list elements must be visible from the -// place where you use this macro, otherwise the proper destructor may not -// be called (a decent compiler should give a warning about it, but don't -// count on it)! -#define WX_CLEAR_LIST(type, list) \ - { \ - type::iterator it, en; \ - for( it = (list).begin(), en = (list).end(); it != en; ++it ) \ - delete *it; \ - (list).clear(); \ - } +template +inline void wxClearList(T& list) +{ + for ( auto& elem: list ) + delete elem; + + list.clear(); +} + +// Deprecated macro, use wxClearList() instead. +#define WX_CLEAR_LIST(type, list) wxClearList(list) // append all element of one list to another one // diff --git a/interface/wx/any.h b/interface/wx/any.h index fa66c46bb7..8eefedbee8 100644 --- a/interface/wx/any.h +++ b/interface/wx/any.h @@ -86,7 +86,7 @@ public: wxVariant does. Note that objects constructed from list-valued variants - require the list to be explicitly cleared using `WX_CLEAR_LIST` + require the list to be explicitly cleared using wxClearList() to avoid leaking memory. This unfortunate behaviour will not be changed to prevent breaking the existing code relying on it. @@ -101,7 +101,7 @@ public: // Clear the list to avoid the memory leak. wxAnyList anyList = any.As(); - WX_CLEAR_LIST(wxAnyList, anyList); + wxClearList(anyList); @endcode */ wxAny(const wxVariant& variant); diff --git a/interface/wx/list.h b/interface/wx/list.h index 4c25e86c9d..961f91aa6a 100644 --- a/interface/wx/list.h +++ b/interface/wx/list.h @@ -475,3 +475,19 @@ public: defining it. */ #define WX_DEFINE_LIST(T, name) + +/** + Deprecated macro equivalent to wxClearList(). + + @deprecated This macro is deprecated, use wxClearList() instead. + */ +#define WX_CLEAR_LIST(type, list) wxClearList(list) + +/** + Function deletes all elements of the given list, which must be pointers. + + This simple helper function just calls delete on each element of the list. + + @since 3.3.0 + */ +template inline void wxClearList(T& list); diff --git a/src/common/filesys.cpp b/src/common/filesys.cpp index aabb9255a1..b0fe391ccc 100644 --- a/src/common/filesys.cpp +++ b/src/common/filesys.cpp @@ -640,7 +640,7 @@ bool wxFileSystem::HasHandlerForPath(const wxString &location) void wxFileSystem::CleanUpHandlers() { - WX_CLEAR_LIST(wxList, m_Handlers); + wxClearList(m_Handlers); } // Returns the native path for a file URL diff --git a/src/common/menucmn.cpp b/src/common/menucmn.cpp index 9464bf6aa2..8ddd8b7b56 100644 --- a/src/common/menucmn.cpp +++ b/src/common/menucmn.cpp @@ -143,7 +143,7 @@ wxCONSTRUCTOR_DUMMY( wxMenuBar ) const wxMenuInfoHelperList& wxMenuBarBase::GetMenuInfos() const { wxMenuInfoHelperList* list = const_cast< wxMenuInfoHelperList* > (& m_menuInfos); - WX_CLEAR_LIST( wxMenuInfoHelperList, *list); + wxClearList(*list); for (size_t i = 0 ; i < GetMenuCount(); ++i) { wxMenuInfoHelper* info = new wxMenuInfoHelper(); @@ -382,7 +382,7 @@ void wxMenuBase::Init(long style) wxMenuBase::~wxMenuBase() { - WX_CLEAR_LIST(wxMenuItemList, m_items); + wxClearList(m_items); } // ---------------------------------------------------------------------------- @@ -901,7 +901,7 @@ wxMenuBarBase::wxMenuBarBase() wxMenuBarBase::~wxMenuBarBase() { - WX_CLEAR_LIST(wxMenuList, m_menus); + wxClearList(m_menus); } // ---------------------------------------------------------------------------- diff --git a/src/common/nbkbase.cpp b/src/common/nbkbase.cpp index 7cc525200f..9b4eccca20 100644 --- a/src/common/nbkbase.cpp +++ b/src/common/nbkbase.cpp @@ -120,7 +120,7 @@ void wxNotebookBase::AddPageInfo( wxNotebookPageInfo* info ) const wxNotebookPageInfoList& wxNotebookBase::GetPageInfos() const { wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos ); - WX_CLEAR_LIST( wxNotebookPageInfoList, *list ); + wxClearList(*list); for( size_t i = 0; i < GetPageCount(); ++i ) { wxNotebookPageInfo *info = new wxNotebookPageInfo(); diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index 978950e53d..6d0f323d58 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -802,7 +802,7 @@ bool wxSizerItem::IsShown() const wxSizer::~wxSizer() { - WX_CLEAR_LIST(wxSizerItemList, m_children); + wxClearList(m_children); } wxSizerItem* wxSizer::DoInsert( size_t index, wxSizerItem *item ) @@ -1080,7 +1080,7 @@ void wxSizer::Clear( bool delete_windows ) DeleteWindows(); // Now empty the list - WX_CLEAR_LIST(wxSizerItemList, m_children); + wxClearList(m_children); } void wxSizer::DeleteWindows() diff --git a/src/common/tbarbase.cpp b/src/common/tbarbase.cpp index 04c8057b9c..0c4cd881b2 100644 --- a/src/common/tbarbase.cpp +++ b/src/common/tbarbase.cpp @@ -529,7 +529,7 @@ bool wxToolBarBase::Realize() wxToolBarBase::~wxToolBarBase() { - WX_CLEAR_LIST(wxToolBarToolsList, m_tools); + wxClearList(m_tools); // notify the frame that it doesn't have a tool bar any longer to avoid // dangling pointers diff --git a/src/html/htmlwin.cpp b/src/html/htmlwin.cpp index 1f5f138fe9..cbca629610 100644 --- a/src/html/htmlwin.cpp +++ b/src/html/htmlwin.cpp @@ -293,7 +293,7 @@ wxCursor *wxHtmlWindow::ms_cursorDefault = nullptr; void wxHtmlWindow::CleanUpStatics() { wxDELETE(m_DefaultFilter); - WX_CLEAR_LIST(wxList, m_Filters); + wxClearList(m_Filters); wxDELETE(m_GlobalProcessors); wxDELETE(ms_cursorLink); wxDELETE(ms_cursorText); diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index 78e22ecc26..0e353176cf 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -2869,8 +2869,8 @@ void wxMSWDCImpl::AddToDCCache(wxDCCacheEntry* entry) void wxMSWDCImpl::ClearCache() { - WX_CLEAR_LIST(wxList, sm_dcCache); - WX_CLEAR_LIST(wxList, sm_bitmapCache); + wxClearList(sm_dcCache); + wxClearList(sm_bitmapCache); } // Clean up cache at app exit diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index adb6170600..166616251f 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -11759,7 +11759,7 @@ bool wxRichTextCommand::Undo() void wxRichTextCommand::ClearActions() { - WX_CLEAR_LIST(wxList, m_actions); + wxClearList(m_actions); } /*! diff --git a/src/richtext/richtextstyles.cpp b/src/richtext/richtextstyles.cpp index 5086477a63..7da4ad0cad 100644 --- a/src/richtext/richtextstyles.cpp +++ b/src/richtext/richtextstyles.cpp @@ -366,10 +366,10 @@ wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxList& list, c /// Delete all styles void wxRichTextStyleSheet::DeleteStyles() { - WX_CLEAR_LIST(wxList, m_characterStyleDefinitions); - WX_CLEAR_LIST(wxList, m_paragraphStyleDefinitions); - WX_CLEAR_LIST(wxList, m_listStyleDefinitions); - WX_CLEAR_LIST(wxList, m_boxStyleDefinitions); + wxClearList(m_characterStyleDefinitions); + wxClearList(m_paragraphStyleDefinitions); + wxClearList(m_listStyleDefinitions); + wxClearList(m_boxStyleDefinitions); } /// Insert into list of style sheets diff --git a/src/x11/textctrl.cpp b/src/x11/textctrl.cpp index a811846c9c..342d901e4a 100644 --- a/src/x11/textctrl.cpp +++ b/src/x11/textctrl.cpp @@ -201,7 +201,7 @@ wxTextCtrl::wxTextCtrl( wxWindow *parent, wxTextCtrl::~wxTextCtrl() { - WX_CLEAR_LIST(wxList, m_undos); + wxClearList(m_undos); } bool wxTextCtrl::Create( wxWindow *parent, @@ -403,7 +403,7 @@ void wxTextCtrl::Clear() SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 ); Refresh(); - WX_CLEAR_LIST(wxList, m_undos); + wxClearList(m_undos); } void wxTextCtrl::Replace(long from, long to, const wxString& value) diff --git a/tests/any/anytest.cpp b/tests/any/anytest.cpp index 463bf2af73..fe1d0d97ab 100644 --- a/tests/any/anytest.cpp +++ b/tests/any/anytest.cpp @@ -657,7 +657,7 @@ void wxAnyTestCase::wxVariantConversions() CPPUNIT_ASSERT(variant[0].GetLong() == 15); CPPUNIT_ASSERT(variant[1].GetString() == "abc"); // Avoid the memory leak. - WX_CLEAR_LIST(wxAnyList, anyList); + wxClearList(anyList); any = wxAny(vCustomType); CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any, wxVariantData*)); diff --git a/tests/benchmarks/htmlparser/htmlpars.cpp b/tests/benchmarks/htmlparser/htmlpars.cpp index 55b09664b1..1072116196 100644 --- a/tests/benchmarks/htmlparser/htmlpars.cpp +++ b/tests/benchmarks/htmlparser/htmlpars.cpp @@ -89,7 +89,7 @@ wx28HtmlParser::~wx28HtmlParser() } delete m_HandlersStack; m_HandlersHash.Clear(); - WX_CLEAR_LIST(wxList, m_HandlersList); + wxClearList(m_HandlersList); delete m_entitiesParser; } diff --git a/tests/lists/lists.cpp b/tests/lists/lists.cpp index eb8e886086..e4fc92910b 100644 --- a/tests/lists/lists.cpp +++ b/tests/lists/lists.cpp @@ -203,7 +203,7 @@ void ListsTestCase::wxListCtorTest() #if !wxUSE_STD_CONTAINERS list1.DeleteContents(true); #else - WX_CLEAR_LIST(wxListBazs, list1); + wxClearList(list1); #endif }