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.
This commit is contained in:
Marek Roszko 2022-03-13 18:40:37 -04:00 committed by Vadim Zeitlin
parent 28ca585d12
commit a3a1f8a2f1

View File

@ -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; }