From 79aea3a9a266abefb7d9b1f4c8ca58e2db861feb Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Wed, 12 Aug 2015 18:43:43 +0200 Subject: [PATCH] Fix inserting items into unsorted wxBitmapComboBox. When wxBitmapComboBox::DoInsertItems() is called from wxBitmapComboBox::RecreateControl() then the existing bitmap array should be reused and new bitmaps shouldn't be allocated. Closes https://github.com/wxWidgets/wxWidgets/pull/71 --- src/msw/bmpcbox.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/msw/bmpcbox.cpp b/src/msw/bmpcbox.cpp index 0954ad7722..35665f61d5 100644 --- a/src/msw/bmpcbox.cpp +++ b/src/msw/bmpcbox.cpp @@ -360,16 +360,33 @@ int wxBitmapComboBox::DoInsertItems(const wxArrayStringsAdapter & items, } else { - const unsigned int countNew = GetCount() + numItems; - - m_bitmaps.Alloc(countNew); - - for ( unsigned int i = 0; i < numItems; i++ ) + if ( GetCount() == m_bitmaps.Count() ) { - m_bitmaps.Insert(new wxBitmap(wxNullBitmap), pos + i); + // Control is in the normal state. + // Just insert new bitmaps into the array. + const unsigned int countNew = GetCount() + numItems; + m_bitmaps.Alloc(countNew); + + for ( unsigned int i = 0; i < numItems; i++ ) + { + m_bitmaps.Insert(new wxBitmap(wxNullBitmap), pos + i); + } + } + else + { + wxASSERT_MSG( GetCount() < m_bitmaps.Count(), + wxS("Invalid wxBitmapComboBox state") ); + // There are less items then bitmaps. + // (This can happen if control is e.g. recreated with RecreateControl). + // In this case existing bitmaps are reused. + // The whole block of inserted items should be within the range + // of indices of the existing bitmap array. + wxASSERT_MSG( pos + numItems <= m_bitmaps.Count(), + wxS("wxBitmapComboBox item index out of bound") ); } - index = wxComboBox::DoInsertItems(items, pos, clientData, type); + index = wxComboBox::DoInsertItems(items, pos, + clientData, type); // This returns index of the last item in the inserted block. if ( index == wxNOT_FOUND )