I use Directx10 in my program. When I try to change vertexes' positions I get an error:
Exception thrown at 0x558CA083 (d3d10core.dll) in NBodyProblemDebug.exe: 0xC0000005: Access violation reading location 0x00000000.
It happens during the execution of the next code during the run-time:
D3D10_MAPPED_TEXTURE3D resourse;
ZeroMemory(&resourse, sizeof(D3D10_MAPPED_TEXTURE3D));
g_pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &resourse.pData); //exception is thrown here
memcpy(resourse.pData, positions, 4 * n_bodies * sizeof(float));
g_pVertexBuffer->Unmap();
where the veкtex buffer is initialized as follows:
float * positions;
ID3D10Buffer* g_pVertexBuffer = nullptr;
D3D10_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof(bd) );
bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = sizeof( SimpleVertex ) * bodies;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
D3D10_SUBRESOURCE_DATA InitData;
ZeroMemory( &InitData, sizeof(InitData) );
InitData.pSysMem = positions;
result = m_device->CreateBuffer( &bd, &InitData, &g_pVertexBuffer );
What one should do to fix it? Is this code an apt equivalent to the one used in Directx11:
D3D11_MAPPED_SUBRESOURCE resourse;
ZeroMemory(&resourse, sizeof(D3D11_MAPPED_SUBRESOURCE));
g_pImmediateContext->Map(g_pVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resourse);
memcpy(resourse.pData, positions, 4 * bodies * sizeof(float));
g_pImmediateContext->Unmap(g_pVertexBuffer, 0);
Your code is not doing any error checking. Whenever a COM API returns a
HRESULT, you must check it for failure. If it was safe to ignore the return value, then it would returnvoid. You should use theSUCCEEDEDorFAILEDmacros to check the results, or adopt something like ThrowIfFailed.If as you note the exception is thrown on the line with
Map, then likely you are missing a failedHRESULTearlier in the program that resulted ing_pImmediateContextstill being nullptr.If once you have checked all the
HRESULTswhere appropriate, the second step for debugging a DirectX application is to enable the DEBUG device. This will then provide additional details about any failures in the debug output window which will help you pinpoint the failure case in your program. See Anatomy of Direct3D 11 Create Device and Direct3D SDK Debug Layer TricksBTW, based on your style you are using raw pointers for your COM interfaces. You should consider adopting a smart-pointer like ComPtr.