GetDC() ReleaseDC()引起的内存泄漏

BUG: GetDC() ReleaseDC()引起的内存泄漏
     今天同事在一个定时器中画图,结果发现内存缓慢增长,怀疑是内存泄漏,查了很久,该释放的GDI资源都释放了的啊,可是还是无法解决,最后把GetDC()放在初始化里面,退出时调用ReleaseDC(),中间处理部分直接使用pDC,内存倒是不增长了,可是一直心里觉得奇怪,又仔细查了下,原来这是MFC的一个BUG.每次调用有4字节的内存泄漏,具体可见下面链接:
 

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:
CDC *pDC;
    RECT rect;

    GetClientRect (&rect);

    for (int i = 0; i < 1000; i++)
    {
        pDC = GetDC ();
        ReleaseDC (pDC);
  }

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;
       RECT rect;
    ::GetClientRect (m_hWnd, &rect);

    for (int i = 0; i < 1000; i++)
    {
        hDC = ::GetDC (m_hWnd);
        ::DrawText (hDC, L"Testing...", 10, &rect, DT_CENTER);
        ::ReleaseDC (m_hWnd, hDC);
    }
  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值