C++/ D2D1 / Unhandled exception thrown: read access violation. **this** was nullptr

Ra' Shon Burns 20 Reputation points
2023-06-05T01:47:56.0533333+00:00

Hello

I am doing Direct2D and Win32 programming in Visual Studio.

When I debug my application, i get this exception in line 2928 of d2d1.h:

Unhandled exception thrown: read access violation. this was nullptr.

If anyone can help, that would be appreciated.

Here is my code:

#include <Windows.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <dwrite.h>
#include <d2d1.h>
#include <d2d1_1.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg)
    {
    case WM_PAINT:
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
};

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Spore Evolved",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    IDXGISwapChain* swapChain;
    ID3D11Device* device;
    ID3D11DeviceContext* context;

    DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
    swapChainDesc.BufferCount = 1;
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.OutputWindow = hwnd;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.Windowed = TRUE; // Set this to FALSE for fullscreen

    D3D11CreateDeviceAndSwapChain(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        0,
        nullptr,
        0,
        D3D11_SDK_VERSION,
        &swapChainDesc,
        &swapChain,
        &device,
        nullptr,
        &context
    );
    // Create the Direct2D factory
    ID2D1Factory1* d2dFactory;
    D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory1), (void**)&d2dFactory);

    // Create the DirectWrite factory
    IDWriteFactory* dwriteFactory;
    DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown**)&dwriteFactory);

    // Get the back buffer
    ID3D11Texture2D* backBuffer;
    swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);

    // Create a DXGI surface from the back buffer
    IDXGISurface* dxgiSurface;
    backBuffer->QueryInterface(__uuidof(IDXGISurface), (void**)&dxgiSurface);
    backBuffer->Release();

    // Create a Direct2D render target from the DXGI surface
    ID2D1RenderTarget* renderTarget;
    D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
        D2D1_RENDER_TARGET_TYPE_DEFAULT,
        D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED)
    );
    d2dFactory->CreateDxgiSurfaceRenderTarget(dxgiSurface, &props, &renderTarget);
    dxgiSurface->Release();

    // Create a white brush
    ID2D1SolidColorBrush* brush;
    renderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &brush);

    // Create a text format
    IDWriteTextFormat* textFormat;
    dwriteFactory->CreateTextFormat(
        L"Arial",
        nullptr,
        DWRITE_FONT_WEIGHT_NORMAL,
        DWRITE_FONT_STYLE_NORMAL,
        DWRITE_FONT_STRETCH_NORMAL,
        72.0f,
        L"",
        &textFormat
    );
    textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
    textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);

    // Create a text layout
    IDWriteTextLayout* textLayout;
    const wchar_t* text = L"Hello, World!";
    dwriteFactory->CreateTextLayout(
        text,
        wcslen(text),
        textFormat,
        800.0f, // Set this to the width of your window
        600.0f, // Set this to the height of your window
        &textLayout
    );

    // Draw the text
    renderTarget->BeginDraw();
    renderTarget->DrawTextLayout(D2D1::Point2F(0.0f, 0.0f), textLayout, brush);
    renderTarget->EndDraw();

    // Present the back buffer
    swapChain->Present(1, 0);
    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,508 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,625 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 83,021 Reputation points
    2023-06-05T04:06:13.0866667+00:00

    renderTarget is NULL

    Use the method with Device Context as explained in MSDN :

    How to render by using a Direct2D device context

    1 person found this answer helpful.
    0 comments No comments