Crash in UpdateRenderTargetView methode DirectX12


So I looked into some tutorials for DirectX12 and when I copied the code that I downloaded from here it worked but when I brought them into a class and wanted to use it, it just crashes in UpdateRenderTargetView method at the m_BackBuffers[i] = backBuffer;

It says: Exception thrown at 0x00007FF831C65FA1 (d3d10warp.dll) in Hazelnut.exe: 0xC0000005: Access violation writing location

The Code:

void D3D12Core::UpdateRenderTargetViews(ComPtr<IDXGISwapChain4> swapChain, ComPtr<ID3D12DescriptorHeap> descriptorHeap)
    auto rtvDescriptorSize = m_Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

    CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(descriptorHeap->GetCPUDescriptorHandleForHeapStart());

    for (int i = 0; i < m_BufferCount; ++i)
        ComPtr<ID3D12Resource> BackBuffer;
        swapChain->GetBuffer(i, IID_PPV_ARGS(&BackBuffer));

        m_Device->CreateRenderTargetView(BackBuffer.Get(), nullptr, rtvHandle);

        m_BackBuffers[i] = BackBuffer;


Class members That I used in function:

 class D3D12Core
    //Some members
    static const uint8_t m_BufferCount = 3;
    ComPtr<ID3D12Resource> m_BackBuffers[m_BufferCount];
    ComPtr<ID3D12Device2> m_Device;
    //Some members

I tried everything that I could but didn't find the cause. Normally it shouldn't crash at all. Please be gentle.I'm new to Stackoverflow. Any help will be appreciated.



D3D12Core Implementation

And I use it like this:

auto commnadQueue = D3D12Core::Get().GetCommandQueue(D3D12_COMMAND_LIST_TYPE_DIRECT);
m_SwapChain = D3D12Core::Get().CreateSwapChain(m_WindowHandle, commnadQueue->GetD3D12CommandQueue(), m_Width, m_Height);
m_RTVDescriptorHeap = D3D12Core::Get().CreateDescriptorHeap(1, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12Core::Get().UpdateRenderTargetViews(m_SwapChain, m_RTVDescriptorHeap);

the UpdateRenderTargetViews function will get call by another function in window class that will be used for WndProc. I didn't write in which class or file this written I don't think it will be necessary.

asked on Stack Overflow Jan 27, 2021 by Amir • edited Jan 30, 2021 by zezanjee

1 Answer


Well after you provided some more source code, I am pretty sure the mistake is here:

m_RTVDescriptorHeap = D3D12Core::Get().CreateDescriptorHeap(1, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

which should actually be

m_RTVDescriptorHeap = D3D12Core::Get().CreateDescriptorHeap(D3D12Core::m_BufferCount, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);

You are offseting the handle in a for loop which goes out of bounds in second iteration.

answered on Stack Overflow Jan 27, 2021 by zezanjee • edited Jan 27, 2021 by zezanjee

User contributions licensed under CC BY-SA 3.0