来自WM_Paint的BeginPaint()函数的内存丢失

时间:2015-01-08 17:12:16

标签: c++ winapi

我看过这个网站和其他网站的类似问题的多个回复,虽然我觉得我已经接近了,但我还是做不到。不过,这可能是一个超级愚蠢的问题。


所以我过去每隔几分钟只调用一次WndProc案例“WM_Paint”(通过InvalidateRect),所以我没有真正注意到泄漏。现在我添加了一些东西,每秒调用它约5次。在那一秒,我的内存使用量跃升约3800k。是的,这引起了注意...... 这是代码:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        // Other cases omitted since we skip them due to "case WM_Paint:".
        case WM_PAINT:
            wndProc_Paint(hwnd);
        break;
        // Other cases omitted since we skip them due to "break;".
    }
    return 0;
}

void wndProc_Paint(HWND hwnd)
{
    g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command
    BITMAP bm;
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps); // <- Breakpoint here while monitoring mem usage shows this is what is adding ~772k per call which never gets released.
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmBoard);
    GetObject(g_hbmBoard, sizeof(bm), &bm);
    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmGreenLight);
    GetObject(g_hbmGreenLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmWorkingLight);
    GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    hbmOld = (HBITMAP)SelectObject(hdcMem, g_hbmWorkingIndicator);
    GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    //DeleteObject(hbmOld);
    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);
    //SelectObject(hdc, hbmOld);
    //ReleaseDC(hwnd, hdc);
    //DeleteDC(hdc);
    //DeleteObject(hdc);

    EndPaint(hwnd, &ps);
}

除了g_hbmWorkingIndicator段和注释掉的部分之外,这是我的WM_Paint在被调用5次/秒之前的样子(甚至在我把它放入它自己的函数之前 - 这是一个单独的问题)。

“HDC hdc = BeginPaint(hwnd,&amp; ps);”是添加内存的地方,它永远不会被释放。每次我们完成这个功能都会发生这种情况。所以我现在尝试解决这个问题。

阅读类似的问题,我认为我需要释放或删除hdc的DC。我不是(仍然不是 - 是的,我已经阅读过MSDN页面)确定SelectObject如何使用这些,所以我尝试使用和不使用ReleaseDC&amp;删除hdc和hdcMem的DC(&amp; both)。我也尝试过DeleteObject for hdc&amp; hdcMem。这些都没有任何影响。

DeleteObject(hbmOld)有效果。 解决了这个问题。除了这是错误的解决方案,因为虽然我没有失控的内存,但我确实有一些视觉效果被错误的视觉效果所取代。 g_hbmGreenLight通常获取g_hbmBoard的图形,而g_hbmWorkingLight变为绿色(g_hbmGreenLight的图形)。移动“DeleteObject(hbmOld);”在EndPaint(〜)之后或尝试使用“SelectObject”只是改变哪些对象被哪个错误的图形替换 - 也;返回内存泄漏。


编辑:为了完整起见,我在这里包含了ConvertIplImageToHBITMAP(IplImage * image)的代码。这完全有可能是罪魁祸首。

HBITMAP ConvertIplImageToHBITMAP(IplImage* pImage)
{
    IplImage* image = (IplImage*)pImage;
    bool imgConverted = false;

    if(pImage->nChannels != 3)
    {
        IplImage* imageCh3 = cvCreateImage(cvGetSize(pImage), 8, 3);
        if(pImage->nChannels==1){cvCvtColor(pImage, imageCh3, CV_GRAY2RGB);}
        image = imageCh3;
        imgConverted = true;
    }

    int bpp = image->nChannels * 8;
    assert(image->width >= 0 && image->height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));
    CvMat dst;
    void* dst_ptr = 0;
    HBITMAP hbmp = NULL;
    unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
    BITMAPINFO* bmi = (BITMAPINFO*)buffer;
    BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

    ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
    bmih->biSize = sizeof(BITMAPINFOHEADER);
    bmih->biWidth = image->width;
    bmih->biHeight = image->origin ? abs(image->height) : -abs(image->height);
    bmih->biPlanes = 1;
    bmih->biBitCount = bpp;
    bmih->biCompression = BI_RGB;

    if (bpp == 8)
    {
        RGBQUAD* palette = bmi->bmiColors;
        int i;
        for (i = 0; i < 256; i++)
        {
            palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = (BYTE)i;
            palette[i].rgbReserved = 0;
        }
    }

    hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0, 0);
    cvInitMatHeader(&dst, image->height, image->width, CV_8UC3, dst_ptr, (image->width * image->nChannels + 3) & -4);
    cvConvertImage(image, &dst, image->origin ? CV_CVTIMG_FLIP : 0);

    if(imgConverted)
    {cvReleaseImage(&image);}

    return hbmp;
}

所以,所有这一切:帮我StackExchange,你是我唯一的希望! ; _;

1 个答案:

答案 0 :(得分:9)

您正在丢失HBITMAP返回的原始SelectObject(hdcMem, g_hbmBoard),因此在调用DeleteDC()之前您没有正确恢复它,因此它会被泄露。每次拨打hbmOld时,您都会覆盖SelectObject()变量,但在重写hbmOld之前,您不会将当前dcMem值恢复为hbmOld。使用SelectObject()时,必须在完成对HDC的更改后恢复原始对象。

请改为尝试:

void wndProc_Paint(HWND hwnd)
{
    g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command

    BITMAP bm;
    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    HBITMAP hbmOld = (HBITMAP) SelectObject(hdcMem, g_hbmBoard); // save the original HBITMAP
    GetObject(g_hbmBoard, sizeof(bm), &bm);
    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmGreenLight); // returns g_hbmBoard, no need to save it to hbmpOld
    GetObject(g_hbmGreenLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmWorkingLight); // returns g_hbmGreenLight, no need to save it to hbmpOld
    GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmWorkingIndicator); // returns g_hbmWorkingLight, no need to save it to hbmpOld
    GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld); // restore the original HBITMAP

    DeleteDC(hdcMem);
    EndPaint(hwnd, &ps);

    // who owns g_hbmBoard? ConvertIplImageToHBITMAP() creates
    // a new HBITMAP, so you need to free it with DeleteObject()
    // before calling ConvertIplImageToHBITMAP() again.  It would
    // be better to create g_hbmBoard one time outside of WM_PAINT,
    // recreate g_hbmBoard only when the source image actually changes,
    // and then re-use g_hbmBoard as-is inside of WM_PAINT.
    // ConvertIplImageToHBITMAP() really does not belong in WM_PAINT...
    //
    //DeleteObject(g_hbmBoard);
}

或者,请改用SaveDC() / RestoreDC()

void wndProc_Paint(HWND hwnd)
{
    g_hbmBoard = ConvertIplImageToHBITMAP(targetBoardImg); //OpenCV command

    BITMAP bm;
    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(hwnd, &ps);
    HDC hdcMem = CreateCompatibleDC(hdc);

    int iOldState = SaveDC(hdcMem); // save everything the HDC currently has selected

    SelectObject(hdcMem, g_hbmBoard);
    GetObject(g_hbmBoard, sizeof(bm), &bm);
    BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmGreenLight);
    GetObject(g_hbmGreenLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 59, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmWorkingLight);
    GetObject(g_hbmWorkingLight, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 94, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, g_hbmWorkingIndicator);
    GetObject(g_hbmWorkingIndicator, sizeof(bm), &bm);
    BitBlt(hdc, screenResW - 129, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    RestoreDC(hdcMem, iOldState); // restore everything the HDC originally had selected

    DeleteDC(hdcMem);
    EndPaint(hwnd, &ps);

    // who owns g_hbmBoard? ConvertIplImageToHBITMAP() creates
    // a new HBITMAP, so you need to free it with DeleteObject()
    // before calling ConvertIplImageToHBITMAP() again.  It would
    // be better to create g_hbmBoard one time outside of WM_PAINT,
    // recreate g_hbmBoard only when the source image actually changes,
    // and then re-use g_hbmBoard as-is inside of WM_PAINT...
    // ConvertIplImageToHBITMAP() really does not belong in WM_PAINT...
    //
    //DeleteObject(g_hbmBoard);
}