WM_Paint的 BeginPaint() 函数的记忆丧失

Memory Loss from WM_Paint's BeginPaint() function

本文关键字:函数 记忆 丧失 BeginPaint Paint WM      更新时间:2023-10-16

我查看了这个网站和其他网站对类似问题的多个回答,虽然我觉得我已经更接近了,但我就是不能完全正确。不过,这可能是一个超级菜鸟的问题。


所以我过去每隔几分钟才把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(&both)和不使用ReleaseDC和DeleteDC(&both)。我还尝试了 hdc 和 hdcMem 的 DeleteObject 。这些都没有任何影响。

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


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

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之前,您不会将当前hbmOld值恢复到dcMem。 使用 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);
}