运行下面这个函数为什么会引起GDI泄漏?如何删除这个GDI泄漏?

云满笔记 2013-12-21 06:09:11

void test()
{
CPrintDialog dlgPaint;
dlgPaint.GetDefaults();
}


如上面所示,我点击一个按钮执行上面的代码,可是每点击一次按钮GDI就会增加2个,郁闷啊,这是什么情况啊,因为我的程序要长时间7X24小时不间断运行,这样下去我的程序铁定会挂的呀,有没有这方面的高手啊,怎么解决我的内存泄漏问题啊?

...全文
161 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
云满笔记 2013-12-23
  • 打赏
  • 举报
回复
引用 7 楼 xiaohuh421 的回复:
不是说你是猜的有GDI漏泄, 而是说"猜测的代码", ::DeleteObject( dlgPaint.m_pd.hDC ); 使用MSDN上例子程序中的方式.
问题已经解决了 给你分
云满笔记 2013-12-22
  • 打赏
  • 举报
回复
引用 5 楼 xiaohuh421 的回复:
你可以试试MSDN中代码的例子, 看有没有内存泄露. 而不是使用你自己猜测的代码.
我不是猜的 而是检测到了GDI泄漏
xiaohuh421 2013-12-22
  • 打赏
  • 举报
回复
你可以试试MSDN中代码的例子, 看有没有内存泄露. 而不是使用你自己猜测的代码.
云满笔记 2013-12-22
  • 打赏
  • 举报
回复
引用 3 楼 xiaohuh421 的回复:
dlg.GetDefaults()) 后会得到一个DC 这个DC需要释放. MSDN中的例子中有说明.
CPrintDialog dlg(FALSE);

if (!dlg.GetDefaults())
{
   AfxMessageBox(_T("You have no default printer!"));
}
else
{
   // attach to the DC we were given
   CDC dc;
   dc.Attach(dlg.m_pd.hDC);

   // ask for the measurements
   int nHorz = dc.GetDeviceCaps(LOGPIXELSX);
   int nVert = dc.GetDeviceCaps(LOGPIXELSY);

   // almost always the same in both directions, but sometimes not!
   CString str;
   if (nHorz == nVert)
   {
      str.Format(_T("Your printer supports %d pixels per inch"), nHorz);
   }
   else
   {
      str.Format(_T("Your printer supports %d pixels per inch ")
         _T("horizontal resolution, and %d pixels per inch vertical ")
         _T("resolution"), nHorz, nVert);
   }

   // tell the user
   AfxMessageBox(str);

   // Note: no need to call Detach() because we want the CDC destructor
   // to call FreeDC() on the DC we borrowed from the common dialog
}
注意代码的最后两句注释. 因为它使用CDC这类, 所以可以自动释放GetDefaut获得的DC. 而你自己什么也没做.
我后来加了如下代码还是没用啊 会有2个GDI未释放:1个enhanced metafile DC和1个 Palette if ( dlgPaint.m_pd.hDC ) { ::DeleteObject( dlgPaint.m_pd.hDC ); }
xiaohuh421 2013-12-22
  • 打赏
  • 举报
回复
dlg.GetDefaults()) 后会得到一个DC 这个DC需要释放. MSDN中的例子中有说明.
CPrintDialog dlg(FALSE);

if (!dlg.GetDefaults())
{
   AfxMessageBox(_T("You have no default printer!"));
}
else
{
   // attach to the DC we were given
   CDC dc;
   dc.Attach(dlg.m_pd.hDC);

   // ask for the measurements
   int nHorz = dc.GetDeviceCaps(LOGPIXELSX);
   int nVert = dc.GetDeviceCaps(LOGPIXELSY);

   // almost always the same in both directions, but sometimes not!
   CString str;
   if (nHorz == nVert)
   {
      str.Format(_T("Your printer supports %d pixels per inch"), nHorz);
   }
   else
   {
      str.Format(_T("Your printer supports %d pixels per inch ")
         _T("horizontal resolution, and %d pixels per inch vertical ")
         _T("resolution"), nHorz, nVert);
   }

   // tell the user
   AfxMessageBox(str);

   // Note: no need to call Detach() because we want the CDC destructor
   // to call FreeDC() on the DC we borrowed from the common dialog
}
注意代码的最后两句注释. 因为它使用CDC这类, 所以可以自动释放GetDefaut获得的DC. 而你自己什么也没做.
云满笔记 2013-12-22
  • 打赏
  • 举报
回复
没有人吗 周末提问就是不给力啊
xiaohuh421 2013-12-22
  • 打赏
  • 举报
回复
不是说你是猜的有GDI漏泄, 而是说"猜测的代码", ::DeleteObject( dlgPaint.m_pd.hDC ); 使用MSDN上例子程序中的方式.
云满笔记 2013-12-21
  • 打赏
  • 举报
回复
进来都有分 大家多多给力啊

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧