From 148cafdcd0ef7c2805d97a1d715aa9bc42708acf Mon Sep 17 00:00:00 2001 From: PB Date: Tue, 4 Apr 2023 13:42:13 +0200 Subject: [PATCH] Fix memory leak in the art provider sample The list client data must be destroyed not only when closing the resource browser dialog (see 754f75c1cd (Fix memory leaks in artprov sample, 2022-01-28) but also whenever the art client is changed. See #23417, #23418. (cherry picked from commit 02cba46643a5988db1b0e803e18e2dc1a55d1d21) --- samples/artprov/artbrows.cpp | 15 ++++++++++----- samples/artprov/artbrows.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/samples/artprov/artbrows.cpp b/samples/artprov/artbrows.cpp index e33319c348..cb0d7281ca 100644 --- a/samples/artprov/artbrows.cpp +++ b/samples/artprov/artbrows.cpp @@ -196,11 +196,7 @@ wxArtBrowserDialog::wxArtBrowserDialog(wxWindow *parent) wxArtBrowserDialog::~wxArtBrowserDialog() { - const int itemCount = m_list->GetItemCount(); - - // item data are set by the ART_ICON macro - for ( int i = 0; i < itemCount; ++i ) - delete reinterpret_cast(m_list->GetItemData(i)); + DeleteListItemData(); } wxSize wxArtBrowserDialog::GetSelectedBitmapSize() const @@ -209,6 +205,14 @@ wxSize wxArtBrowserDialog::GetSelectedBitmapSize() const return wxSize(size, size); } +void wxArtBrowserDialog::DeleteListItemData() +{ + const int itemCount = m_list->GetItemCount(); + + // item data are set by the ART_ICON macro + for ( int i = 0; i < itemCount; ++i ) + delete reinterpret_cast(m_list->GetItemData(i)); +} void wxArtBrowserDialog::SetArtClient(const wxArtClient& client) { @@ -221,6 +225,7 @@ void wxArtBrowserDialog::SetArtClient(const wxArtClient& client) long sel = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); if (sel < 0) sel = 0; + DeleteListItemData(); m_list->DeleteAllItems(); FillBitmaps(img, m_list, index, client, wxSize(16, 16)); m_list->AssignImageList(img, wxIMAGE_LIST_SMALL); diff --git a/samples/artprov/artbrows.h b/samples/artprov/artbrows.h index 3827762538..effbc4e1ac 100644 --- a/samples/artprov/artbrows.h +++ b/samples/artprov/artbrows.h @@ -34,6 +34,8 @@ private: wxSize GetSelectedBitmapSize() const; + void DeleteListItemData(); + wxListCtrl *m_list; wxStaticBitmap *m_canvas; wxStaticText *m_text;