【发布时间】:2014-06-28 15:07:38
【问题描述】:
我正在使用以下功能实时处理图像。该函数使用计时器每 10 秒调用一次。
问题是我得到一个断言失败,并且无法弄清楚确切的问题。我为 ImageDC 尝试了 CImage::ReleaseDC() 和 DeleteDC() 但没有运气。
有什么想法吗?
LRESULT CAutodetectDialog::AutoscanPatterns(WPARAM, LPARAM)
{
HWND hwnd = ::FindWindow(NULL, windowTitle);
if (hwnd != NULL)
for (int i=0; i<N_NUMBERS; i++)
{
CImage image;
image.Create(dbParams.width, dbParams.height, 24);
CImageDC imageDC(image);
::SetWindowOrgEx(imageDC, db.topLeft.x, dbParams.topLeft.y + i * dbParams.height, NULL);
::PrintWindow(hwnd, imageDC, PW_CLIENTONLY);
// Process the image - processing takes < 1 sec
// and the image parameter is not being changed
SaveImagePatterns(&image);
} // <------------- This line fails , must be the destructor
// of CImage : atlimage.h Line 884, m_hDC == 0
// m_hDC is not NULL in the code
return 0;
}
// Process the image - processing takes < 1 sec
// and the image parameter is not changed
void CAutodetectDialog::SaveImagePatterns(const CImage* image)
{
.........
}
这是 atlimage.h 中失败的代码:
inline HBITMAP CImage::Detach() throw()
{
HBITMAP hBitmap;
ATLASSUME( m_hBitmap != NULL );
ATLASSUME( m_hDC == NULL ); // <------ This guy
hBitmap = m_hBitmap;
...
...
return( hBitmap );
}
更新:注释掉对函数 SaveImagePatterns() 的调用后,断言失败没有发生。所以问题一定出在那个函数上,尽管 CImage 参数作为 const 传递。
【问题讨论】:
-
The function is called every 10 seconds using a timer.假设该功能由于某种原因需要超过 10 秒。是什么阻止了函数在执行时被调用?另外,请说明SaveImagePatterns应该做什么。请注意,您的参数是局部变量的地址,当该块退出时,局部变量会自动销毁。 -
对,我应该提到执行时间小于 1 秒,为了避免此类问题,将计时器设置为 10 秒。
-
SaveImagePatterns似乎是最有可能的罪魁祸首。显示它的代码 -
@Wartin - 该解决方案是创可贴。 10 秒问题的真正解决方案是使用适当的同步,即临界区或互斥锁。
-
@Wartin - "........" 没有告诉我们您如何使用该参数或该函数的作用。请将真实代码发布到函数中,而不是点。