我使用Direct2D在VS2010上渲染动画。但是当我创建超过100个动画对象(限制量是可变的)时, 输出窗口显示错误消息:
StarrySky.exe中的第一次机会异常0x7607C42D:Microsoft C ++ 异常:内存位置为0x001C9580的_com_error。“
但是,错误不会中断程序,动画对象似乎是正确的。 我跟踪代码并在调用
后找出错误消息print
hr = m_pD2DFactory->CreateHwndRenderTarget(renderTargetProperties, D2D1::HwndRenderTargetProperties(this->m_hWnd, size), &m_pRT);
但是返回hr是S_OK。有谁知道原因或解决方案?
答案 0 :(得分:0)
在Direct2D中,确保在设备相关资源之前实例化设备无关资源(在此构造函数中的某些内容 - 在此代码示例中为Init_D2D())。例如,
// In your header files
ID2D1Factory* pD2DFactory; // device independent
ID2D1HwndRenderTarget* pRenderTarget;
// In the source files
void Init_D2D() // probably in constructor
{
HRESULT hr = D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory);
}
void Create_D2D_Device_Dep() //before drawing
{
if(pRenderTarget == NULL) //if it exists, do not create again
{
HRESULT hr = pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(m_hWnd,
D2D1::SizeU((UINT32)m_pRect.right, (UINT32)m_pRectWindow.bottom)),
&pRenderTarget);
}
}
OnPaint()方法应该是这样的,
void CPlotterView :: OnPaint_D2D() {
HRESULT hr;
hr = Create_D2D_Device_Dep();
if (SUCCEEDED(hr))
{
//pRenderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
pRenderTarget->BeginDraw();
D2D1_SIZE_F renderTargetSize = pRenderTarget->GetSize();
pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::LightYellow));
// Drawlines or ellipses using pRenderTarget->DrawEllipse() or something
hr = pRenderTarget->EndDraw();
}
if (hr == D2DERR_RECREATE_TARGET)
{
Free_D2D_Device_Dep();
}
else
{
::ValidateRect(m_hWnd, NULL);
}
}
最后,请不要忘记以您打开的顺序关闭/释放资源。