如何释放HFONT对象的内存 [英] How do i free memory for a HFONT object

查看:124
本文介绍了如何释放HFONT对象的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GDI泄漏的小问题,我想知道某人对如何解决此问题的看法.说我有一个类,它包含特定于创建和处理窗口的数据,例如:

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


因此,主要的问题是,在销毁时如何释放分配给hFont参数的内存?我应该获取窗口的设备上下文并使用SelectObject()函数,以便在此之后释放它并为该函数调用该函数


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.

推荐答案

hFont内存归系统所有,在您调用时将被释放DeleteObject()(或DeleteFont()).为什么需要复制LOGFONT结构以便创建hFont对象,只使用原始对象即可.
The hFont memory is owned by the system and will be freed when you call DeleteObject() (or DeleteFont()). Why do you need to make a copy of the LOGFONT structure in order to create the hFont object, just use the original.


我继承自它,并且我需要一个成员LOGFONT变量.因此我可以在析构函数的主体中将其删除吗?因为我尝试过并且实际的清理操作未正确执行.感谢您的回答.
I inherit from it and i need a member LOGFONT variable.So can i delete it in the body of the destructor ?Because i tried and the actual cleanup isn''t performed properly.And thanks for the answer.


class Wnd
{
  HWND     hWnd;
  HFONT    hFont;
  LOGFONT  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)
  {
    memcpy(&LogFont,&lf,sizeof(LOGFONT));
    if(hFont) DeleteObject(hFont);
    hFont = CreateFontIndirect (&LogFont);
    SendMessage(hWnd,WM_SETFONT,(WPARAM)(hFont?hFont:GetStockObject(ANSI_VAR_FONT),(LPARAM)1);
  }
  Wnd()
  {
    hWnd  = 0;
    hFont = 0;
    // ...
  }
  ~Wnd()
  {
    //i don''t think this would work since i haven''t used the SelectObject function
    if(hFont) DeleteObject(hFont);
  }
};


问候,这看起来更好.


That looks better, Regards.


这篇关于如何释放HFONT对象的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆