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

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


所以我过去每隔几分钟只调用一次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 . 我没有't (still aren' t - 是的我've read the MSDN page) sure how SelectObject works with these, so I'已经为hdc和hdcMem尝试了没有ReleaseDC和DeleteDC(&both) . 我也为hdc和hdcMem尝试了DeleteObject . 这些都不会影响到.752826_ .

DeleteObject(hbmOld)有效 . 它 solved the issue. 除了它已经失去控制的内存,我确实有一些视觉效果被错误的视觉效果所取代 . g_hbmGreenLight通常得到g_hbmBoard 's graphic while g_hbmWorkingLight turns green (g_hbmGreenLight' s图形) . 将"DeleteObject(hbmOld);"移动到EndPaint(〜)之后或尝试使用"SelectObject"只是更改哪些对象被哪个错误的图形替换 - 也;返回内存泄漏 .


EDIT: 为了完整起见,我在这里包含了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)

2 years ago

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