LoadBitmap在程序运行一段时间后返回NULL

LoadBitmap returns NULL after program has been running for a while

本文关键字:返回 NULL 一段时间 运行 程序 LoadBitmap      更新时间:2023-10-16

我正在开发一个程序,该程序具有反映系统状态的bmp图像(因此,这些灯可以根据状态而更改)。这些图像位于主窗口上,旁边是一个带有几个选项卡的选项卡窗口。当我第一次运行我的程序时,一切都很好。我可以更改选项卡和仍然在其右侧的图像,正确地反映状态。我让我的程序运行了一整夜,发现当我回来时,程序仍然可以正常运行(我所有的按钮和选项卡都可以工作),但我的图像消失了。

我调试了我的WM_PAINT,注意到这个代码

Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(LightSource[i]));

现在返回NULL

所以我的问题是,我如何才能让它一直正常工作?有什么想法吗?

int LightSource[12] = { IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1, IDB_BITMAP1 };
case WM_PAINT:
    {
        HBITMAP Light = NULL;   //Bitmaps for the light indications
        //Prepares for painting window
         hdc = BeginPaint(hwnd, &ps);
        //Retrieves the coordinates of the windows client area
         GetClientRect(hwnd, &rc);
        //creates a copy of the memory device context 
        HDC hdcDouble = CreateCompatibleDC(hdc);
        HBITMAP bmOld;
        for (int i = 0; i < 12; i++)    //For all indicator lights
        {
            Light = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(LightSource[i]));         //Get a bitmap of the picture to be updated
            bmOld = (HBITMAP)SelectObject(hdcDouble, Light);                                    //Get a handle to the image being replaced
            BitBlt(hdc, xLight[i], yLight[i], rc.right, rc.bottom, hdcDouble, 0, 0, SRCCOPY);   //Bit block transfer of the bitmap color data
        }
        SelectObject(hdcDouble, bmOld);
        DeleteDC(hdcDouble);
        EndPaint(hwnd, &ps);
        //Set some window text, no need to show
        DeleteObject(Light);
        break;
    }

回答这个问题,这样它就不会一直打开:由于您没有释放位图,所以您的程序中似乎存在资源泄漏。使用位图后释放位图。