c++ - WM_Paint 的 BeginPaint() 函数导致内存丢失

标签 c++ winapi

我查看了本网站和其他网站对类似问题的多个回复,虽然我觉得自己离答案越来越近了,但我还是不太对。不过,这可能是一个 super 菜鸟的问题。


所以我以前只每隔几分钟调用一次 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, &ps);”这一行是添加内存的地方,它永远不会被释放。每次我们通过函数时都会发生这种情况。所以现在我尝试解决这个问题。

阅读类似的问题,我假设我需要释放或删除 hdc 的 DC。我不是(仍然不是 - 是的,我已经阅读了 MSDN 页面)确定 SelectObject 如何与这些一起工作,所以我已经为 hdc 和 hdcMem 尝试了使用和不使用 ReleaseDC 和 DeleteDC(以及两者)。我还为 hdc 和 hdcMem 尝试了 DeleteObject。这些都没有任何影响。

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,你是我唯一的希望! ;_;

最佳答案

您正在丢失 SelectObject(hdcMem, g_hbmBoard) 返回的原始 HBITMAP,因此在调用 DeleteDC()< 之前您没有正确恢复它,因此它被泄露了。每次调用 SelectObject() 时都会覆盖 hbmOld 变量,但不会将当前的 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);
}

关于c++ - WM_Paint 的 BeginPaint() 函数导致内存丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27845782/

相关文章:

c++ - Apple LLVM 4.2 段错误使用基于范围的循环和引用

c++ - Windows C++ 管道创建黑色大终端窗口

c++ - 如何检测子窗口即时是否按下了ctrl键。 richedit 是焦点?

c++ - 尝试清除特定票证时,LsaCa​​llAuthenticationPackage 返回 ERROR_INVALID_PARAMETER 87 (0x57)

c++ - 除非首先创建,否则首先创建的对话框将变得无响应?

c++ - 如何以及何时从 malloc 返回?

c++ - 类范围内的二维 vector

c++ - 运行函数 GetVolumeInformation() 时我得到非常随机的结果

c++ - 如何在 64 位 Windows 中制作 OpenGL 应用程序?

立即计算的 lambda 的 C++ 范围规则