关闭

Wince GDI资源泄露可能原因

标签: wincemicrosoftdeletefunctionleakclass
2030人阅读 评论(1) 收藏 举报
分类:
Wince GDI资源泄露可能原因

在网上找了很多人的博客来看,在各种论坛里面看高手的回复,把Wince GDI资源泄露的可能原因总结了一下
(1)不要用CDC做直接用HDC
(2)OnEraseBkgnd擦除背景函数没用,只有一句return true;,所以把Invalidate();改成Invalidate(0);
(3)    BUG: Memory leaks when you use the CWnd class versions of the GetDC method and the ReleaseDC method
    When you run code that calls the CWnd::GetDC function followed by the CWnd::ReleaseDC function, a memory leak of 4 bytes occurs. The cause of this bug is currently unknown. To avoid this problem, do not use the CWnd class versions of the GetDC method and the ReleaseDC method. Use the GetDC function and the ReleaseDC function. Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
    Steps to Reproduce the Behavior
In a method that is a part of a class that is derived from CWnd, insert the following code in your application:
  1. CDC *pDC;  
  2. RECT rect;  
  3.   
  4. GetClientRect (&rect);  
  5.   
  6. for (int i = 0; i < 1000; i++)  
  7. {  
  8.     pDC = GetDC ();  
  9.     ReleaseDC (pDC);  
  10. }  

    If you run this code and then check the system memory before and after you run the code, you notice that the system memory leaks four bytes per iteration. If you change the code to the following code, the memory leak does not occur:
 HDC hDC;
  1. RECT rect;  
  2. ::GetClientRect (m_hWnd, &rect);  
  3.   
  4. for (int i = 0; i < 1000; i++)  
  5. {  
  6.     hDC = ::GetDC (m_hWnd);  
  7.     ::DrawText (hDC, L"Testing...", 10, &rect, DT_CENTER);  
  8.     ::ReleaseDC (m_hWnd, hDC);  
  9. }  
(4) 如果过于频繁的使用CreateCompatibleDC这种的话可以改为成员变量,在析构函数中释放。
(5) 创建的Gdiobj如果执行了selectobject,一般要这样删除,例如pOldFont=selectobject(&m_fontSongTi);在删除时要先pDC->SelectObject(pOldFont);再删除GDI资源,m_fontSongTi.deleteobject(); 

附:GDI使用的几个注意点:

1、Create出来的GDI对象,要用Deleteobject释放, create出来的DC,要用DeleteDc释放,getDC得出的DC,用ReleaseDc释放。

2、先create后delete, create1,create2, delete2,delete1的顺序。

3、画图步骤

a 创建一个内存兼容dc(CreateCompatibleDC)
b.创建一个内存兼容bitmap(CreateCompatibleBitmap)
c.关联创建的内存兼容dc和bitmap(SelectObject)
d.画图
e.BitBlt到目的dc上
f.断开内存兼容dc和bitmap关联(SelectObject)
g.销毁内存兼容bitmap
h.销毁内存兼容dc


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:299765次
    • 积分:3707
    • 等级:
    • 排名:第8728名
    • 原创:56篇
    • 转载:150篇
    • 译文:0篇
    • 评论:34条
    文章分类
    最新评论