WinCE下GetDC内存泄露的bug

本文描述了在微软Windows CE系统中使用MFC时出现的一个内存泄漏问题,具体表现为调用CWnd类的GetDC和ReleaseDC方法导致每次迭代泄漏4字节内存。文章提供了复现该行为的代码示例,并提出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天偶然发现微软wince系统下mfc的一个bug,微软的网站上也有记录,如下:

    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:

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

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值