问题
I have a function, inside a class called ModelClass, that does the following:
bool ModelClass::SetVertices(ID3D11Device* device, VertexType* vertices)
{
// Error catching variable
HRESULT result;
// Setup the vertex buffer description
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(vertices)*24;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
//vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
D3D11_SUBRESOURCE_DATA vertexData;
ZeroMemory(&vertexData, sizeof(vertexData));
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Create the Vertex Buffer
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if (FAILED(result))
{
return false;
}
return true;
}
Running this, gives me an error message saying "Unhandled exception at 0x00E445A2 in Engine.exe: 0xC0000005: Access violation reading location 0x00000000.", with visual studio pointing at the result = ...
line. I can not, for the life of me, figure out why it's doing this. I don't even know what else to post, so I'll just enumerate things I find relevant:
ModelClass::ModelClass(ID3D11Device* device, VertexType* vertices, unsigned long* indices)
{
m_vertexBuffer = 0;
m_indexBuffer = 0;
SetVertices(device, vertices);
SetIndices(device, indices);
}
- The class is being defined with
ModelClass* cube1 = new ModelClass(d3d11Device, v, indices);
, in the global scope (for now) with the constructor in the code box above. - The
m_vertexBuffer
variable is declared asID3D11Buffer *m_vertexBuffer
in the class header file, and initialized to zero in the class constructor. - DirectX 11 on Visual Studio 2013, Windows 8.1 64bit
- If I copy the contents of the SetVertices function into my main file (which holds most of the setting up), it immediately works without this error. It only happens if I call CreateBuffer from inside the class. This works the exactly the same way for my index buffer, which indicates it's not a problem with the vertex structure
- All functions and variables of the class are public
- CreateBuffer still throws that exception even if one, more or all of it's arguments are set to NULL (i.e. I don't think it's a problem with m_vertexBuffer, the data, or buffer description)
- My main guess is that it has something to do with how classes use these functions, but I can't find anything wrong with my code
Please, if you would like to see the rest of the files, just comment and I will pastebin the entire thing if it's necessary, this is driving me up the wall already
回答1:
Fantastic site, RasterTek.
Anyways, the problem is that you're initializing this thing in the global scope - before d3d11Device
is initialized. Everything declared in the global scope is created instantly when the program starts in an arbitrary order. On the other hand, d3d11Device
is not initialized when the program starts.
Take it out of the global scope or re-add the Initialize
function from the tutorial. Problem solved.
来源:https://stackoverflow.com/questions/21223337/createbuffer-throwing-an-access-violation-reading-location