Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to remove a handle HFONT from memory?

How to remove a handle HFONT from memory?

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsperformancehelptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daed
    wrote on last edited by
    #1

    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

    G T D 3 Replies Last reply
    0
    • D 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

      G Offline
      G Offline
      Gert Boddaert
      wrote on last edited by
      #2

      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. --------------------------------------------------

      1 Reply Last reply
      0
      • D 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

        T Offline
        T Offline
        Ted Ferenc
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D 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

          D Offline
          D Offline
          Daed
          wrote on last edited by
          #4

          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

          T 1 Reply Last reply
          0
          • D 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

            T Offline
            T Offline
            Ted Ferenc
            wrote on last edited by
            #5

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

            T D 2 Replies Last reply
            0
            • T Ted Ferenc

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

              T Offline
              T Offline
              Ted Ferenc
              wrote on last edited by
              #6

              Sorry must learn to type if(m_hFont) DeleteObject(m_hFont)

              1 Reply Last reply
              0
              • T Ted Ferenc

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

                D Offline
                D Offline
                Daed
                wrote on last edited by
                #7

                I tried it, but i will still have one font handle in the memory!

                T 1 Reply Last reply
                0
                • D Daed

                  I tried it, but i will still have one font handle in the memory!

                  T Offline
                  T Offline
                  Ted Ferenc
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups