From a3a1f8a2f14038fcabbf923843d0ce3eea6f6762 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sun, 13 Mar 2022 18:40:37 -0400 Subject: [PATCH] Release HDC in WindowHDC destructor even if HWND is invalid It is possible to use WindowHDC as ScreenHDC by passing a NULL HWND to its ctor and we still need to release the HDC allocated in this case in the dtor, so do _not_ check for HWND being valid there. This fixes a bad resource leak in wxWindow::GetDPI() when it's called for a window without a valid handle. Closes #22193. Closes #22194. --- include/wx/msw/private.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/wx/msw/private.h b/include/wx/msw/private.h index 7e0deb8b46..982714f7bc 100644 --- a/include/wx/msw/private.h +++ b/include/wx/msw/private.h @@ -437,13 +437,14 @@ private: wxDECLARE_NO_COPY_CLASS(ScreenHDC); }; -// the same as ScreenHDC but for window DCs +// the same as ScreenHDC but for window DCs (and if HWND is NULL, then exactly +// the same as it) class WindowHDC { public: WindowHDC() : m_hwnd(NULL), m_hdc(NULL) { } WindowHDC(HWND hwnd) { m_hdc = ::GetDC(m_hwnd = hwnd); } - ~WindowHDC() { if ( m_hwnd && m_hdc ) { ::ReleaseDC(m_hwnd, m_hdc); } } + ~WindowHDC() { if ( m_hdc ) { ::ReleaseDC(m_hwnd, m_hdc); } } operator HDC() const { return m_hdc; }