c++directx-9vertex-buffer

DirectX 9 Vertex Buffer crashes program when using D3DUSAGE_DYNAMIC flag


I have the following code:

DirectX::device->CreateVertexBuffer(sizeof(VERTEX) * vertexCount, D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, VERTEXFORMAT, D3DPOOL_MANAGED, &vertexBuffer, NULL);

VOID* vertexLocking;
vertexBuffer->Lock(0, 0, (void**)&vertexLocking, 0);
memcpy(vertexLocking, vertices, sizeof(VERTEX) * vertexCount);
vertexBuffer->Unlock();

The problem is that the program keeps breaking on the Lock() function for the vertex Buffer. The error i'm getting is "access violation". But the thing is this worked fine if I put 0 in for the flag.

And because I wanted the buffer's vertices position to change, I tried to change it to D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY and now it stopped working. Why is this?


Solution

  • I found out what the problem is: Because I'm using D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, it does notwork with D3DPOOL_MANAGED. I switched it to D3DPOOL_DEFAULT and it worked.