Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

access violation - 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;

        rtvHandle.Offset(rtvDescriptorSize);
    }
}

Class members That I used in function:

 class D3D12Core
{
 public:
    //Some members
    static const uint8_t m_BufferCount = 3;
    ComPtr<ID3D12Resource> m_BackBuffers[m_BufferCount];
 private:
    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.

Edit:

D3D12Core

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.

question from:https://stackoverflow.com/questions/65918243/crash-in-updaterendertargetview-methode-directx12

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...