HDC内存泄漏(释放HDC/删除HDC)
HDC memory leak (release HDC/ delete hdc)
我遇到HDC内存泄漏的问题。你们能检查一下我是否正确发布/删除HDC吗?
谢谢!
BITMAP bm;
HBITMAP hbmap;
HBITMAP hBitmapOld;
BITMAPINFO bmi;
HDC hdcShot;
while(true)
{
if (!TakeScreenshot(GameWindow, bm, hbmap, bmi, hdcShot, hBitmapOld, appWnd))
break;
HBITMAP hbmapNew = CreateCompatibleBitmap(hdcShot, rcWindow.right - rcWindow.left, rcWindow.bottom - rcWindow.top);
HDC hdcShotNew = CreateCompatibleDC(hdcShot);
HBITMAP OldBmp = (HBITMAP)SelectObject(hdcShotNew, hbmapNew);
BitBlt(hdcShotNew, 0, 0, rcWindow.right - rcWindow.left/*Window WIDTH*/, rcWindow.bottom - rcWindow.top/*Window HEIGHT*/
, hdcShot, 0, 0, SRCCOPY);
pPixels = new RGBQUAD[bm.bmWidth * bm.bmHeight];
if (!pPixels) return false;
SelectObject(hdcShotNew, OldBmp);
if (!GetDIBits(hdcShotNew, hbmapNew, 0, bm.bmHeight, pPixels, &bmi, DIB_RGB_COLORS))
{
DeleteDC(hdcShot);
delete[] pPixels;
return false;
}
// dont mind about this
ScanContents scanContentsMain(bm, rcWindow, pPixels);
// dont mind about this
ScanBMPHorizontal(&scanContentsMain);
//free memory - I might have cleared the memory incorrectly here! Please check!
free(pPixels);
SelectObject(hdcShot, hBitmapOld);
DeleteDC(hdcShot);
DeleteObject(hbmapNew);
DeleteDC(hdcShotNew);
}
TakeScreenShot Func(不是很重要,但它显示了一些变量是如何初始化的)
bool TakeScreenshot(std::string WindowToFind, BITMAP &bm, HBITMAP &hbmap, BITMAPINFO &bmi, HDC &hdcShot, HBITMAP &hBitmapOld, HWND &hwnd)
{
RECT rc;
GetWindowRect(hwnd, &rc);
hdcShot = CreateCompatibleDC(0);
hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/);
SelectObject(hdcShot, hbmap);
BitBlt(hdcShot, 0, 0, rc.right - rc.left/*Window WIDTH*/, rc.bottom - rc.top/*Window HEIGHT*/
, GetDC(0), rc.left, rc.top, SRCCOPY);
//Ignore this
if (!GetObject(hbmap, sizeof(BITMAP), (LPSTR)&bm))
return false;
int bitsPerPixel = bm.bmBitsPixel;
//Ignore this
if (bitsPerPixel != 32 || bm.bmPlanes != 1)
return false;
//Don't mind about this too much
SetupBitmapInfo(bmi, bm.bmWidth, bm.bmHeight, bitsPerPixel);
return true;
}
我向删除者核实了一下,发现我正在与HDC泄漏作斗争。我不确定我做错了什么。
此处存在资源泄漏:
hbmap = CreateCompatibleBitmap(GetDC(0), rc.right - rc.left, rc.bottom - rc.top);
你应该换成
HDC hdc = GetDC(0);
CreateCompatibleBitmap(hdc, rc.right - rc.left, rc.bottom - rc.top);
...
ReleaseDC(0, hdc);//call this before exiting the function
free(pPixels)
是错误的(尽管在这种情况下它仍然可以清除)。用delete[]pPixels
替换free(pPixels)
更改此项:
SelectObject(hdcShot, hBitmapOld);
DeleteDC(hdcShot);
DeleteObject(hbmapNew);
DeleteDC(hdcShotNew);
对此:
SelectObject(hdcShot, OldBmp);
DeleteObject(hbmapNew);
DeleteObject(hBitmapOld);
DeleteDC(hdcShot);
DeleteDC(hdcShotNew);
相关文章:
- 将数组的地址分配给变量并删除
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C/C++编译器通常会删除重复的库吗
- 从链接列表c++中删除一个项目
- C++如何通过用户输入删除列表元素
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 使用函数"remove"删除重复元素
- 如何从多映射中删除特定的重复项
- 运算符C++ "delete []"仅删除 2 个前值
- 删除指向指针的指针是运行时错误吗
- 将指针设置为"nullptr"并不能防止双重删除?
- 为什么示例代码访问IUnknown中已删除的内存
- 如何通过 getter 函数删除矢量的元素?
- 从控制台中删除最后打印的元素
- C++中的线程安全删除
- 如何从存储在std::映射中的std::集中删除元素
- HDC内存泄漏(释放HDC/删除HDC)