开发者

Win32 font resource leak

开发者 https://www.devze.com 2023-03-22 21:22 出处:网络
I have a small matter of GDI leak and i wanted to know someone\'s opinion on how to solve this.Say i have a class that enfolds data specific to creating and handling a window ex:

I have a small matter of GDI leak and i wanted to know someone's opinion on how to solve this.Say i have a class that enfolds data specific to creating and handling a window ex:

class Wnd  {
   HWND hWnd;
   HFONT hFont;
   LO开发者_Python百科GFONT LogFont;
   //etc
public:
   //constructors and member functions
   //The following function atempts to change the font of the window
   //pointed to by the hWnd parameter
   void ChangeFont (const LOGFONT& lf)  {
      std::memcpy (&LogFont,&lf,sizeof(LOGFONT));
      hFont=CreateFontIndirect (&LogFont);
      SendMessage (hWnd,WM_SETFONT,(WPARAM) hFont,(LPARAM) 1);
    }
   ~Wnd ()  {
      //i don't think this would work since i haven't used the SelectObject function
      DeleteObject ((HGDIOBJ) hFont);
    }
 };

So the main question is , at destruction time how do i release the memory allocated to the hFont parameter?Should i get a device context of the window and use the SelectObject () function so that after that i could release it calling the function for the old font and use DeleteObject () to free the memory?Thanks a lot.


So the main question is , at destruction time how do i release the memory allocated to the hFont parameter?

You use DeleteObject() per documentation for CreateFontIndirect() and the WM_SETFONT message.

Should i get a device context of the window and use the SelectObject () function so that after that i could release it calling the function for the old font and use DeleteObject () to free the memory?

This should not be necessary, as long as your painting routine correctly restores the old font somehow after the routine is done with the font.

0

精彩评论

暂无评论...
验证码 换一张
取 消