GetDC() ReleaseDC()引起的記憶體流失

來源:互聯網
上載者:User
BUG: GetDC() ReleaseDC()引起的記憶體流失      今天同事在一個定時器中畫圖,結果發現記憶體緩慢增長,懷疑是記憶體流失,查了很久,該釋放的GDI資源都釋放了的啊,可是還是無法解決,最後把GetDC()放在初始化裡面,退出時調用ReleaseDC(),中間處理部分直接使用pDC,記憶體倒是不增長了,可是一直心裡覺得奇怪,又仔細查了下,原來這是MFC的一個BUG.每次調用有4位元組的記憶體流失,具體可見下面連結: http://support.microsoft.com/kb/819635/zh-cn   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);    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.