locked
Is this Memory leak ?? RRS feed

  • Question

  • Hello Everyone ..

    Am new to this forum & VC++ MFC .. Am just slightly confused with memory leak, Please suggest/guide me on this.  AM taking screenshot & code looks like this:

    void CMyClass::Take_Screenshot(){

    CImage image;

    CImage image2;

    HDC ScreenDC = ::GetDC(NULL);

    HDC hDC = CreateCompatibleDC(ScreenDC);

    HBITMAP hBitmap = CreateCompatibleBitmap(ScreenDC, 100, 100);

    HBITMAP hOldBitmap = (HBITMAP)SelectObject(hDC, hBitmap);

    image.Create(110, 60, 24);

    image2.Create(76, 40, 24);

    CImageDC imageDC(image);

    CImageDC imageDC2(image2);

    ::BitBlt(imageDC,   0,   0, 110, 60, ScreenDC,  25 ,  208, SRCCOPY);

    ::BitBlt(imageDC2,  0 ,  0, 76, 40, ScreenDC,   780 ,  182, SRCCOPY);

    HRESULT hr  =  image.Save("E:\\Test1.tiff", ImageFormatTIFF);

    HRESULT hr2 =  image2.Save("E:\\Test2.tiff", ImageFormatTIFF);

    SelectObject(hDC, hOldBitmap);

    DeleteDC(hDC);

    ::ReleaseDC(NULL, ScreenDC);

    Am running this code continously, like this:

    void CMyClass::OnBnClickedOK()

    {     

    for(;;)

    {

    Take_Screenshot();

    }

    }

    When i run like this, it works fine. But when i take a close look to the Task manager, my application memory goes on increases. While cross checking in Take_Screenshot() function, I commented each & every line & run my application. Interesting fact is: when i comment these two lines & run my application:

    HRESULT hr  =  image.Save("E:\\Test1.tiff", ImageFormatTIFF);

    HRESULT hr2 =  image2.Save("E:\\Test2.tiff", ImageFormatTIFF);

    In Task manager memory will not increase, it will be steady. And if i include these two statements & run my application... Memory goes on increases in task manager. Am confused, Is this memory leak ?? OR because am running Take_Screenshot() function continouously, thats why memory increases ? Please guide me on this..

    Thank you all :)

    Friday, October 5, 2012 5:05 AM

Answers

  • Hello Everyone ..

    I guess i found the solution :) I just used the Memory leak detection code in my application & i fond that there is no memory leak with Take_Screenshot() function... And also while running the Take_Screenshot() function continously at the end i included Sleep(2000); Somewhat like this:

    for(;;)
    {
    Take_Screenshot();
    Sleep(2000);
    }
    In Task Manger, Memory is constant... But,Still slight variation is there.. I think while running forever, we need to perform idle processing ( i.e., Sleep(1000) ), so that CPU can release the threads ..

    Friday, October 5, 2012 9:45 AM

All replies

  • Hello Everyone ..

    Am new to this forum & VC++ MFC .. Am just slightly confused with memory leak, Please suggest/guide me on this.  AM taking screenshot & code looks like this:

    void CMyClass::Take_Screenshot(){

    CImage image;

    CImage image2;

    HDC ScreenDC = ::GetDC(NULL);

    HDC hDC = CreateCompatibleDC(ScreenDC);

    HBITMAP hBitmap = CreateCompatibleBitmap(ScreenDC, 100, 100);

    .

    .

    .

    .

    .

    SelectObject(hDC, hOldBitmap);

    DeleteDC(hDC);

    ::ReleaseDC(NULL, ScreenDC);

    Am running this code continously, like this:

    void CMyClass::OnBnClickedOK()

    {     

    for(;;)

    {

    Take_Screenshot();

    }

    }


    In Task manager memory will not increase, it will be steady. And if i include these two statements & run my application... Memory goes on increases in task manager. Am confused, Is this memory leak ??

    Hi,

      I think it is Memory leak issue..........Why because you are creating compatibleDC and CompatibleBitmap.

    HDC hDC = CreateCompatibleDC(ScreenDC);

    HBITMAP hBitmap = CreateCompatibleBitmap(ScreenDC, 100, 100);

    But you delete only ComatibleDC

    DeleteDC(hDC);

    What about Bitmap......??? May be this will create memory leak.Delete that bitmap object using Deleteobject  and see even after ,that issue happening or not

     Thank You


    Gopinath.S

    Friday, October 5, 2012 6:20 AM
  • Hi Gopinath.. Thanks for replying.. As per as ur suggestion i added the DeleteObject(hBitmap);  Tested it.. Still memory in task manager goes on increases..
    Friday, October 5, 2012 6:54 AM
  • Hello Everyone ..

    I guess i found the solution :) I just used the Memory leak detection code in my application & i fond that there is no memory leak with Take_Screenshot() function... And also while running the Take_Screenshot() function continously at the end i included Sleep(2000); Somewhat like this:

    for(;;)
    {
    Take_Screenshot();
    Sleep(2000);
    }
    In Task Manger, Memory is constant... But,Still slight variation is there.. I think while running forever, we need to perform idle processing ( i.e., Sleep(1000) ), so that CPU can release the threads ..

    Friday, October 5, 2012 9:45 AM
  • The memory leak detection features in MFC analyze the heap only. They do not detect leaks of handles or GDI objects. If you have additional code in your Take_Screenshot function I suggest you study it very closely.

    You should also realize that TaskManager is not a programmer's tool. The Performance Monitor (in Control Panel) provides much better detail.

    Friday, October 5, 2012 2:16 PM
  • Just for your information. The memory leak occurs when Private Working Set  increases constantly not the memory column in task manager. You can check the handles also using task manager. For enabling these columns View->Select Columns


    Thanks, Renjith V R

    Friday, October 5, 2012 5:16 PM