(Win32 refresh) DialogUtil

Fixes a 20 year old font leak issue which can be avoided by storing the font handle in a unique_ptr.
This commit is contained in:
sukibaby
2025-01-20 09:44:24 -08:00
committed by teejusb
parent 641fac232a
commit 54232bed78
+15 -4
View File
@@ -5,6 +5,7 @@
#include "archutils/Win32/ErrorStrings.h" #include "archutils/Win32/ErrorStrings.h"
#include <cmath> #include <cmath>
#include <memory>
// Create*Font copied from MFC's CFont // Create*Font copied from MFC's CFont
@@ -43,6 +44,17 @@ static HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName)
return ::CreatePointFontIndirect(&logFont); return ::CreatePointFontIndirect(&logFont);
} }
struct FontDeleter
{
void operator()(HFONT font) const
{
if (font)
{
::DeleteObject(font);
}
}
};
void DialogUtil::SetHeaderFont(HWND hdlg, int nID) void DialogUtil::SetHeaderFont(HWND hdlg, int nID)
{ {
ASSERT(hdlg != nullptr); ASSERT(hdlg != nullptr);
@@ -50,10 +62,9 @@ void DialogUtil::SetHeaderFont( HWND hdlg, int nID )
HWND hControl = ::GetDlgItem(hdlg, nID); HWND hControl = ::GetDlgItem(hdlg, nID);
ASSERT(hControl != nullptr); ASSERT(hControl != nullptr);
// TODO: Fix font leak static std::unique_ptr<std::remove_pointer<HFONT>::type, FontDeleter> hfont(CreatePointFont(16 * 10, "Arial Black"));
const int FONT_POINTS = 16;
HFONT hfont = CreatePointFont( FONT_POINTS*10, "Arial Black" ); ::SendMessage(hControl, WM_SETFONT, (WPARAM)hfont.get(), TRUE);
::SendMessage( hControl, WM_SETFONT, (WPARAM)hfont, TRUE );
} }
void DialogUtil::LocalizeDialogAndContents( HWND hdlg ) void DialogUtil::LocalizeDialogAndContents( HWND hdlg )