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);