【发布时间】:2019-02-12 22:29:57
【问题描述】:
我正在使用PrintWindow() 截取应用程序的屏幕截图。该应用程序包含一个列表视图,一段时间后,列表不再更新。只有当我在列表中选择一个条目时,它才会更新该条目的名称。我的假设是 ListView 的窗口不会以某种方式失效,但这只是一个猜测。我在截屏后尝试拨打InvalidateRect(),但这也无济于事。
我认为这一定是资源泄漏的原因,但我将所有需要的资源封装在一个类中,该类会自动处理释放它们。
struct DrawingSurface
{
DrawingSurface(const DrawingSurface&) = delete;
DrawingSurface& operator=(const DrawingSurface&) = delete;
// Window is a custom class, but it's not really important here.
DrawingSurface(const Window& window)
: hwnd(window.handle()), pixels(0), windowDC(0), memoryDC(0), bitmap(0), previous(0)
{
// Get window size.
Rect clientRect = window.getClientRect();
width = clientRect.width();
height = clientRect.height();
// Create DCs.
windowDC = ::GetDC(window.handle());
if(windowDC == NULL)
return;
memoryDC = ::CreateCompatibleDC(windowDC);
if(memoryDC == NULL)
return;
// Create bitmap.
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biWidth = width;
bitmapInfo.bmiHeader.biHeight = -height;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biSizeImage = width * height * 4;
bitmap = ::CreateDIBSection(windowDC, &bitmapInfo, DIB_RGB_COLORS, (void**)&pixels, 0, 0);
if(bitmap == NULL)
return;
previous = ::SelectObject(memoryDC, bitmap);
}
~DrawingSurface()
{
if(windowDC != NULL)
::ReleaseDC(hwnd, windowDC);
if(previous != NULL && previous != HGDI_ERROR && memoryDC != NULL)
::SelectObject(memoryDC, previous);
if(memoryDC != NULL)
::DeleteDC(memoryDC);
if(bitmap != NULL)
::DeleteObject(bitmap);
}
bool valid() const
{
return width * height > 0
&& previous != NULL
&& previous != HGDI_ERROR
&& windowDC != NULL
&& memoryDC != NULL
&& bitmap != NULL;
}
int width, height;
HWND hwnd;
HDC windowDC;
HDC memoryDC;
HBITMAP bitmap;
RGBQUAD* pixels;
BITMAPINFO bitmapInfo;
private:
HGDIOBJ previous;
};
然后我用这个绘图表面用这个函数截屏:
bool Screenshot::take(const Window& window)
{
m_width = 0; m_height = 0;
DrawingSurface surface(window);
if(!surface.valid())
return false;
if(PrintWindow(surface.hwnd, surface.memoryDC, PW_CLIENTONLY) == 0)
return false;
if(GdiFlush() == 0)
return false;
// Set attributes.
m_hwnd = surface.hwnd;
m_width = surface.width;
m_height = surface.height;
// Copy pixels.
m_pixels.resize(surface.width * surface.height, { 0, 0, 0, 0 });
memcpy(&m_pixels[0], surface.pixels, surface.bitmapInfo.bmiHeader.biSizeImage);
return true;
}
我看不出我可以在哪里泄漏任何资源。任何其他想法为什么我上面描述的可能会发生?
任何帮助表示赞赏,谢谢。
【问题讨论】:
-
仅供参考,
previous在您的场景中永远不可能是HGDI_ERROR,因此检查它没有意义。如果SelectObject()失败,您应该在构造函数中检查previous==null。更重要的是,如果构造函数确实失败了,您应该抛出异常而不是让类处于不稳定状态。这也可以让你摆脱valid()。 -
使用任务管理器诊断句柄泄漏,为用户对象和 GDI 对象添加列。
-
minimal reproducible example 将大大提高您解决此问题的机会。
-
可能不是原因,但你不应该坚持
windowDC(以及相关的hwnd)。在构造函数末尾调用ReleaseDC(window.handle(), windowDC)以安全资源。 -
我没有看到这里显示的代码存在重大问题,因此问题的根源可能位于您未显示的代码中..
标签: c++ winapi screenshot