CreateHwndRenderTarget 抛出异常:读取访问冲突。 **这是 nullptr
CreateHwndRenderTarget throwing exception: read access violation. **this** was nullptr
我最近一直在使用 this 教程学习 Win32 API。我一直在尝试创建一个 window,它使用基于它的 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 .
您的 pFactory 引用为空。但是你确实初始化了它,为什么它是空的?因为您的代码已损坏,所以它会这样做:
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;
只需在WM_DESTROY
前加一个break
即可。
我最近一直在使用 this 教程学习 Win32 API。我一直在尝试创建一个 window,它使用基于它的 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 .
您的 pFactory 引用为空。但是你确实初始化了它,为什么它是空的?因为您的代码已损坏,所以它会这样做:
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;
只需在WM_DESTROY
前加一个break
即可。