From c7efab6d8a2a18ecea8d7b016915b94697a03165 Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sun, 17 Feb 2019 19:49:34 +0100 Subject: [PATCH] Fix using invalid HFONT in wxGetTextMetrics() If the window has no valid font, GetFont() returns a temporary font. Extend this font lifetime, so the HFONT remains valid till the end of the function. --- src/msw/window.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/msw/window.cpp b/src/msw/window.cpp index 5f8ef7791d..58d88811d2 100644 --- a/src/msw/window.cpp +++ b/src/msw/window.cpp @@ -7421,7 +7421,12 @@ static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win) #if !wxDIALOG_UNIT_COMPATIBILITY // and select the current font into it - HFONT hfont = GetHfontOf(win->GetFont()); + + // Note that it's important to extend the lifetime of the possibly + // temporary wxFont returned by GetFont() to ensure that its HFONT remains + // valid. + const wxFont& f(win->GetFont()); + HFONT hfont = GetHfontOf(f); if ( hfont ) { hfont = (HFONT)::SelectObject(hdc, hfont);