From b9a1931394faffc3fb95441238c82cd4e77cd15e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 12 Dec 2021 23:39:48 +0000 Subject: [PATCH] Fix using MSW wxStaticBitmap with custom wxBitmapBundles wxBitmapBundle-related changes introduced a bug when wxStaticBitmap could be using an invalid handle (which manifested itself in not showing any image on screen) if it was using wxBitmapBundle whose GetBitmap() returned a wxBitmap that wasn't cached inside wxBitmapBundle itself. In this case, this wxBitmap wasn't referenced anywhere after being associated with wxStaticBitmap and so was destroyed, invalidating the HBITMAP used by the native static control. Fix this by keeping a copy of the bitmap in wxStaticBitmap itself. This is not the most efficient, but is the simplest, solution. --- include/wx/msw/statbmp.h | 4 +++- src/msw/statbmp.cpp | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/wx/msw/statbmp.h b/include/wx/msw/statbmp.h index 015b2d44ae..384343517b 100644 --- a/include/wx/msw/statbmp.h +++ b/include/wx/msw/statbmp.h @@ -122,8 +122,10 @@ private: // we can have either an icon or a bitmap: if m_icon is valid, it is used, - // otherwise m_bitmapBundle defined in the base class is used if it is valid + // otherwise we use m_bitmap which is itself obtained from m_bitmapBundle + // defined in the base class wxIcon m_icon; + wxBitmap m_bitmap; // handle used in last call to STM_SETIMAGE WXHANDLE m_currentHandle; diff --git a/src/msw/statbmp.cpp b/src/msw/statbmp.cpp index 87c223b1eb..dffa7835d5 100644 --- a/src/msw/statbmp.cpp +++ b/src/msw/statbmp.cpp @@ -160,6 +160,8 @@ void wxStaticBitmap::Init() void wxStaticBitmap::Free() { + m_bitmap.UnRef(); + MSWReplaceImageHandle(0); if ( m_ownsCurrentHandle ) @@ -266,7 +268,9 @@ void wxStaticBitmap::DoUpdateImage(const wxSize& sizeOld, bool wasIcon) else #endif // wxUSE_WXDIB { - // Just use the HBITMAP as is. + // Just use the HBITMAP as is, but also make a copy of the bitmap + // to ensure that HBITMAP remains valid for as long as we need it + m_bitmap = bitmap; m_currentHandle = bitmap.GetHandle(); } }