2016-07-29 1 views
3

이 HDC 메모리 누출에 문제가 있습니다. 내가 HDC를 올바르게 릴리스/삭제하는지 확인해 주시겠습니까?HDC 메모리 누출 (HDC 릴리즈/hdc 삭제)

감사합니다.

BITMAP bm; 
HBITMAP hbmap; 
HBITMAP hBitmapOld; 
BITMAPINFO bmi; 
HDC hdcShot; 

...

while(true) 
{ 
    if (!TakeScreenshot(GameWindow, bm, hbmap, bmi, hdcShot, hBitmapOld, appWnd)) 
        break; 

     HBITMAP hbmapNew = CreateCompatibleBitmap(hdcShot, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top); 

     HDC hdcShotNew = CreateCompatibleDC(hdcShot); 

     HBITMAP OldBmp = (HBITMAP)SelectObject(hdcShotNew, hbmapNew); 


     BitBlt(hdcShotNew, 0, 0, rcWindow.right - rcWindow.left/*Window WIDTH*/, rcWindow.bottom - rcWindow.top/*Window HEIGHT*/ 
      , hdcShot, 0, 0, SRCCOPY); 


     pPixels = new RGBQUAD[bm.bmWidth * bm.bmHeight]; 
     if (!pPixels) return false; 

     SelectObject(hdcShotNew, OldBmp); 


     if (!GetDIBits(hdcShotNew, hbmapNew, 0, bm.bmHeight, pPixels, &bmi, DIB_RGB_COLORS)) 
     { 
      DeleteDC(hdcShot); 
      delete[] pPixels; 

      return false; 
     } 



// dont mind about this 
     ScanContents scanContentsMain(bm, rcWindow, pPixels); 
// dont mind about this 
     ScanBMPHorizontal(&scanContentsMain); 




     //free memory - I might have cleared the memory incorrectly here! Please check! 
     free(pPixels); 
     SelectObject(hdcShot, hBitmapOld); 
       DeleteDC(hdcShot); 
       DeleteObject(hbmapNew); 
       DeleteDC(hdcShotNew); 
} 

TakeScreenShot Func을 (정말 중요하지 않습니다하지만 일부 변수가 초기화되는 방법을 보여줍니다)는

bool TakeScreenshot(std::string WindowToFind, BITMAP &bm, HBITMAP &hbmap, BITMAPINFO &bmi, HDC &hdcShot, HBITMAP &hBitmapOld, HWND &hwnd) 
{ 
    RECT rc; 
    GetWindowRect(hwnd, &rc); 
    hdcShot = CreateCompatibleDC(0); 
    hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/); 

    SelectObject(hdcShot, hbmap); 


    BitBlt(hdcShot, 0, 0, rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/ 
     , GetDC(0), rc.left, rc.top, SRCCOPY); 

//Ignore this 
    if (!GetObject(hbmap, sizeof(BITMAP), (LPSTR)&bm)) 
     return false; 
    int bitsPerPixel = bm.bmBitsPixel; 


//Ignore this 
    if (bitsPerPixel != 32 || bm.bmPlanes != 1) 
     return false; 

//Don't mind about this too much 
    SetupBitmapInfo(bmi, bm.bmWidth, bm.bmHeight, bitsPerPixel); 


    return true; 

} 

나는 deleakers으로 확인하고 난 것을 발견 HDC 누출로 고생하고있다. 나는 내가 뭘 잘못했는지 모르겠다.

HDC hdc = GetDC(0); 
CreateCompatibleBitmap(hdc, rc.right - rc.left, rc.bottom - rc.top); 
... 
ReleaseDC(0, hdc);//call this before exiting the function 

free(pPixels) 잘못에 (여전히이 경우 정리 있지만)

hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left, rc.bottom - rc.top); 

당신은 변경해야합니다 :

+0

delete[]pPixels

변경이와 free(pPixels)를 교체합니다. 초기화되지 않은 변수를 삭제하고, DC로 초기화되지 않은 개체를 선택하고, 삭제할 예정이 아닌 다른 개체를 삭제합니다. 이 코드가 전혀 작동합니까? –

+0

그럴 수 있습니다. 이것은 전체 소스 코드의 일부일뿐입니다. 그것은 실제로 꽤 잘 작동합니다. 약간의 유출 된 메모리가 시간이 지남에 따라 합산됩니다. – user3518291

+0

@BarmakShemirani 제 무료 메모리 기능을 살펴보십시오. 나는 내가 기억을 틀리게 해방했을지도 모른다라고 생각한다. [Added TakeScreenShot function] – user3518291

답변

4

는 현재 리소스 누수가. 이에

SelectObject(hdcShot, hBitmapOld); 
DeleteDC(hdcShot); 
DeleteObject(hbmapNew); 
DeleteDC(hdcShotNew); 

: 당신은 무작위로 다른 기능을 요구하고있다

SelectObject(hdcShot, OldBmp); 
DeleteObject(hbmapNew); 
DeleteObject(hBitmapOld); 
DeleteDC(hdcShot); 
DeleteDC(hdcShotNew); 
+0

나는이 대답이 그것을 요약 할 수 있다고 생각한다. 나는 내일 내 컴퓨터로 돌아올 때 그것을 시험 할 것이다. 고맙습니다! – user3518291