When you call CWnd::SetFont()
, the window you assigned the font to does not take ownership of the font. You are responsible to delete the font, but only when it is no longer in use.
As often, the documentation of the underlying Windows API, which is WM_SETFONT
, provides more information than the MFC documentation:
The application should call the DeleteObject function to delete the
font when it is no longer needed; for example, after it destroys the
control.
As you are using the CFont
class, you don't have to explicitly call DeleteObject()
. CFont
is a RAII class that automatically destroys its associated resource in its destructor.
All you have to care about is the scope of the CFont
instance. Currently you create a local variable of CFont
in the OnInitDialog
method. Even when you remove the explicit DeleteObject
call, the font will be destroyed when OnInitDialog
returns and the window you assigned the font to now refers to an invalid font handle.
Solution
Declare an instance of CFont
as a member variable of the Account
class:
class Account : public CDialogEx
{
public:
// Some stuff
private:
CFont m_signInFont;
};
In OnInitDialog
you have to use the member variable instead of the local variable and remove the DeleteObject
call:
BOOL Account::OnInitDialog() {
CDialogEx::OnInitDialog();
VERIFY(m_signInFont.CreatePointFont(160, _T("Arial")));
SignInStatic.SetFont(&m_signInFont);
return TRUE;
}
Now the font object will exist for the whole livetime of the dialog, which includes its children. You could even assign it to other children, as needed.