为什么CreateCompatibleBitmap后约一千执行失败? [英] Why does CreateCompatibleBitmap fail after about a thousand executions?

查看:153
本文介绍了为什么CreateCompatibleBitmap后约一千执行失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个屏幕捕获/备案申请。这里的code,捕捉画面并将其保存的部分:

I am trying to write a screen capture/record application. Here's the part of the code that captures the screen and saves it:

width = GetSystemMetrics(SM_CXMAXTRACK)+8;
height = GetSystemMetrics(SM_CYMAXTRACK)-8;

hwindowDC=GetDC(GetDesktopWindow());
hwindowCompatibleDC=CreateCompatibleDC(hwindowDC);
SetStretchBltMode(hwindowCompatibleDC,COLORONCOLOR);  

// create a bitmap
hbwindow = CreateCompatibleBitmap( hwindowDC, width, height);
cout << " handle to newly created bitmap: " << hbwindow << "\n";

SelectObject(hwindowCompatibleDC, hbwindow); //copy from hwindowCompatibleDC to hbwindow
StretchBlt( hwindowCompatibleDC, 0,0, width, height, hwindowDC, 0, 0,width,height, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !

src.create(height,width,CV_8UC4);   
src.empty();
GetDIBits(hwindowCompatibleDC,hbwindow,0,height,src.data,(BITMAPINFO *)&bi,DIB_RGB_COLORS); 


DeleteDC(hwindowCompatibleDC); 
DeleteObject(hbwindow);

一千元左右的重复后,我的COUT账单会显示新创建的句柄作为又称0000000000000000。空值。我的应用程序运行正常,直到这一点。结果
我删除创建的DC,每次位图,这样就没有内存泄漏。任务管理器中也证实了不存在内存泄漏。所以,发生了什么?

After a thousand or so repetitions, my cout statement will show the newly created handle as 000000000000000 aka. NULL. My app works fine until that point.
I am deleting the created DC and bitmap each time so there is no memory leak. Task Manager also confirms there is no memory leak. So what is happening?

感谢您的人谁可以帮助这一点。

Thank you for anyone who can assist in this.

推荐答案

作为评价所提到的,这两个问题是1)你没有释放从桌面窗口直流得到,2)你没有选择原始位删除前回兼容的DC。这两个导致GDI资源泄漏,这将导致你描述的症状。

As mentioned in the comments, the two problems were 1) you weren't releasing the DC obtained from the desktop window, and 2) you weren't selecting the original bitmap back into the compatible DC before deleting it. Both of these cause a GDI resource leak which will lead to the symptoms you describe.

低于固定code:

width = GetSystemMetrics(SM_CXMAXTRACK)+8;
height = GetSystemMetrics(SM_CYMAXTRACK)-8;

hwindowDC=GetDC(GetDesktopWindow());
hwindowCompatibleDC=CreateCompatibleDC(hwindowDC);
SetStretchBltMode(hwindowCompatibleDC,COLORONCOLOR);  

// create a bitmap
hbwindow = CreateCompatibleBitmap( hwindowDC, width, height);
cout << " handle to newly created bitmap: " << hbwindow << "\n";

// SAVE OLD BITMAP
HGDIOBJ hOldBmp = SelectObject(hwindowCompatibleDC, hbwindow); //copy from hwindowCompatibleDC to hbwindow
StretchBlt( hwindowCompatibleDC, 0,0, width, height, hwindowDC, 0, 0,width,height, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !

src.create(height,width,CV_8UC4);   
src.empty();
GetDIBits(hwindowCompatibleDC,hbwindow,0,height,src.data,(BITMAPINFO *)&bi,DIB_RGB_COLORS); 

// RESTORE OLD BITMAP
SelectObject(hwindowCompatibleDC, hOldBmp);
DeleteDC(hwindowCompatibleDC); 
DeleteObject(hbwindow);

// RELEASE WINDOW DC
ReleaseDC(GetDesktopWindow(), hwindowDC);

您也应该做适当的错误检查(像的GetDC CreateCompatibleDC 通话等,可以失败并返回NULL )。

You should also be doing proper error checking (as calls like GetDC, CreateCompatibleDC etc can fail and return NULL).

这篇关于为什么CreateCompatibleBitmap后约一千执行失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆