热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

WinceGDI资源泄露可能原因

WinceGDI资源泄露可能原因在网上找了很多人的博客来看,在各种论坛里面看高手的回复,把WinceGDI资源泄露的可能原因总结了一下(1)不要用CDC
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:
[cpp] view plaincopy
  1. CDC *pDC;  
  2. RECT rect;  
  3.   
  4. GetClientRect (&rect);  
  5.   
  6. for (int i &#61; 0; i < 1000; i&#43;&#43;)  
  7. {  
  8.     pDC &#61; 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;
[cpp] view plaincopy
  1. RECT rect;  
  2. ::GetClientRect (m_hWnd, &rect);  
  3.   
  4. for (int i &#61; 0; i < 1000; i&#43;&#43;)  
  5. {  
  6.     hDC &#61; ::GetDC (m_hWnd);  
  7.     ::DrawText (hDC, L"Testing...", 10, &rect, DT_CENTER);  
  8.     ::ReleaseDC (m_hWnd, hDC);  
  9. }  


(4) 如果过于频繁的使用CreateCompatibleDC这种的话可以改为成员变量&#xff0c;在析构函数中释放。

(5) 创建的Gdiobj如果执行了selectobject&#xff0c;一般要这样删除&#xff0c;例如pOldFont&#61;selectobject&#xff08;&m_fontSongTi&#xff09;;在删除时要先pDC->SelectObject(pOldFont);再删除GDI资源&#xff0c;m_fontSongTi.deleteobject(); 




附&#xff1a;GDI使用的几个注意点&#xff1a;


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

2、先create后delete&#xff0c; create1&#xff0c;create2&#xff0c; delete2&#xff0c;delete1的顺序。

3、画图步骤

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




推荐阅读
author-avatar
zqb_0125
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有