Memory leak due to GetDC () ReleaseDC ()

Source: Internet
Author: User
memory leak due to BUG:GETDC () ReleaseDC ( )Today colleagues in a timer to draw, the results found that memory is slow growth, suspected memory leaks, check for a long time, the release of GDI resources are released Ah, but still cannot solve, finally put GetDC () in the initialization, the exit call ReleaseDC (), Intermediate processing part of the direct use of the PDC, memory is not growing, but always feel strange, and carefully check the next, the original this is a bug in MFC. Each call has a 4-byte memory leak, specifically the following link: http://support.microsoft.com/kb/ 819635/zh-cn Steps to reproduce the BehaviorIn a method, which is a part of a class this is the derived from CWnd, insert the following code in your application:
CDC *PDC;
    Rect rect;

    GetClientRect (&rect);

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

If you run this code and then check the system memory before and after your run the code, you notice 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 < i++)
    {
        HDC =:: GetDC (M_hwnd);
        ::D rawtext (HDC, L "Testing ...", &rect, Dt_ CENTER);
        :: ReleaseDC (M_hwnd, HDC);
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.