How to remove a handle HFONT from memory?
-
I am using the following to get a handle for the font that I need to write with. HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); My problem is that I have to delete the handle after using it to free the memory, but when I use DeleteObject(hNewFont) the font is not drawn correctly. How can I remove the font handle from the memory that will not affect my drawing. Daed
-
I am using the following to get a handle for the font that I need to write with. HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); My problem is that I have to delete the handle after using it to free the memory, but when I use DeleteObject(hNewFont) the font is not drawn correctly. How can I remove the font handle from the memory that will not affect my drawing. Daed
You probably still use the font (draw text with it) after you deleted the HFONT handle. Maybe you selected it? The solution is to DeleteObject the HFONT handle only after you unselected it in any window using it, e.g when the window is destroyed. -------------------------------------------------- If my messages appear curt, I apologize. I try to be brief to save your time as well as mine. --------------------------------------------------
-
I am using the following to get a handle for the font that I need to write with. HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); My problem is that I have to delete the handle after using it to free the memory, but when I use DeleteObject(hNewFont) the font is not drawn correctly. How can I remove the font handle from the memory that will not affect my drawing. Daed
I assume the font is being draw correctly if you don't delete it? If so, it sounds like you are calling DeleteObject before you have finished drawing the text. This usually happens becasue you have done something like PostMessage to draw the text then immediately afterwards called DeleteObject. I tend to do this, in "pseudo" code, when changing fonts hFont = CreateFont hFoldOld = SelectObject(hFont drawtext SelectObject(hFoldOld DeleteObject(hFont
-
I am using the following to get a handle for the font that I need to write with. HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); My problem is that I have to delete the handle after using it to free the memory, but when I use DeleteObject(hNewFont) the font is not drawn correctly. How can I remove the font handle from the memory that will not affect my drawing. Daed
I pass the hNewFont as a parameter in a function SetMyFont, this function sets a member variable called m_hFont to hNewFont as follows: void CMyClass::SetMyFont(HFONT hNewFont) { m_hFont = hNewFont; } then later I use the member variable m_hFont to draw the text. My exact code is: HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); if (hNewFont) { SetMyFont(hNewFont); } I thought that there is no harm in deleting the handle here because I will use the copy of it, and I delete the m_hFont in the destructor. Can you help me in this? Daed
-
I pass the hNewFont as a parameter in a function SetMyFont, this function sets a member variable called m_hFont to hNewFont as follows: void CMyClass::SetMyFont(HFONT hNewFont) { m_hFont = hNewFont; } then later I use the member variable m_hFont to draw the text. My exact code is: HFONT hNewFont = ::CreateFont(iHeight, 0, 0, 0, (IsBold? FW_BOLD: FW_NORMAL), IsItalic, IsUnderline, IsStrikeOut, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, FontName); if (hNewFont) { SetMyFont(hNewFont); } I thought that there is no harm in deleting the handle here because I will use the copy of it, and I delete the m_hFont in the destructor. Can you help me in this? Daed
This statement m_hFont = hNewFont does not "make a copy" of the font, all you have done is assigned the variable m_hFont to hNewFont. m_hFont is a HANDLE it just points to some memory location, i.e. the same memory location as hNewFont. So basically don't delete hNewFont. I suggest in the construtor m_hFont = NULL; then if(m_hFont) DeleteObject(hFont) m_hFont = NULL; hNewFont = ::CreateFont if (hNewFont) { SetMyFont(hNewFont); }
-
This statement m_hFont = hNewFont does not "make a copy" of the font, all you have done is assigned the variable m_hFont to hNewFont. m_hFont is a HANDLE it just points to some memory location, i.e. the same memory location as hNewFont. So basically don't delete hNewFont. I suggest in the construtor m_hFont = NULL; then if(m_hFont) DeleteObject(hFont) m_hFont = NULL; hNewFont = ::CreateFont if (hNewFont) { SetMyFont(hNewFont); }
Sorry must learn to type if(m_hFont) DeleteObject(m_hFont)
-
This statement m_hFont = hNewFont does not "make a copy" of the font, all you have done is assigned the variable m_hFont to hNewFont. m_hFont is a HANDLE it just points to some memory location, i.e. the same memory location as hNewFont. So basically don't delete hNewFont. I suggest in the construtor m_hFont = NULL; then if(m_hFont) DeleteObject(hFont) m_hFont = NULL; hNewFont = ::CreateFont if (hNewFont) { SetMyFont(hNewFont); }
-
If is difficult to answer your question, without seeing the code. I assume you are failing to delete a font you have created? You could have called CreateFont twice, thereby overwritting the previous font value? Assuming, again, it is all in one class. Constructor m_hFont = NULL CreatingFont if(m_hFont) DeleteObject(m_hFont) m_hFont = NULL; m_hFont = CreateFont( Destructor if(m_hFont) DeleteObject(m_hFont) This way you are only using m_hFont, so it is easier to check what you are doing. Also check the error returns from the functions.