我最近一直在使用this教程来学习Win32 API。我一直在尝试创建一个基于Direct2D的显示圆形的窗口。但是,程序不断抛出异常:
Exception thrown: read access violation.
**this** was nullptr.
VS 2019然后在本节中打开d2d1.h:
CreateHwndRenderTarget(
CONST D2D1_RENDER_TARGET_PROPERTIES &renderTargetProperties,
CONST D2D1_HWND_RENDER_TARGET_PROPERTIES &hwndRenderTargetProperties,
_COM_Outptr_ ID2D1HwndRenderTarget **hwndRenderTarget
)
{
return CreateHwndRenderTarget(&renderTargetProperties, &hwndRenderTargetProperties, hwndRenderTarget);
}
这是我的代码中导致此问题的部分:
HRESULT CreateGraphicsResources(HWND hWnd) {
HRESULT hr = S_OK;
if (pRenderTarget == NULL) {
RECT rc;
GetClientRect(hWnd, &rc);
D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom);
hr = pFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(hWnd, size),
&pRenderTarget);
if (SUCCEEDED(hr)) {
const D2D1_COLOR_F color = D2D1::ColorF(1.0f, 1.0f, 0);
hr = pRenderTarget->CreateSolidColorBrush(color, &pBrush);
if (SUCCEEDED(hr))
{
CalculateLayout();
}
}
}
return hr;
}
这是我调用函数的地方
case WM_PAINT:
{
HRESULT hr = CreateGraphicsResources(hWnd);
if (SUCCEEDED(hr))
{
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
pRenderTarget->BeginDraw();
pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue));
pRenderTarget->FillEllipse(ellipse, pBrush);
hr = pRenderTarget->EndDraw();
if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET)
{
DiscardGraphicsResources();
}
EndPaint(hWnd, &ps);
}
}
那么,此异常是什么意思,是什么导致它被抛出? 另外,我该如何解决? 感谢您的帮助。 完整文件可用here。
答案 0 :(得分:1)
您的pFactory引用为null。但是您要初始化它,为什么它为null?由于您的代码已损坏,因此可以这样做:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
... some code
case WM_SIZE:
{
... some code
}
// there is no break here!
// first call to WM_SIZE falls down to WM_DESTROY which destroys your pFactory...
case WM_DESTROY:
DiscardGraphicsResources();
SafeRelease(&pFactory);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
只需在break
之前添加WM_DESTROY
。