Sign in to follow this  
LevyDee

Hitting Unhandled exception on IDirect3DVertexBuffer9::Unlock()

Recommended Posts

LevyDee    376
As the title says, I am hitting an exception, but I cant figure out why. I am writing a function to subdivide the information in a vertex buffer(double the number of triangles for each level of subdivision)

[code]
void SubDivideSurface(int levels, IDirect3DDevice9* device)
{
//Testing. Assuming levels is 1

//Copy vertex data
void* vertData;
_vertexBuffer->Lock(0, 0, &vertData, 0);
LWVERTEX* vertDataCopy = new LWVERTEX[sizeof(LWVERTEX) * _verticeCount];
memcpy(vertDataCopy, vertData, sizeof(LWVERTEX) * _verticeCount);
_vertexBuffer->Unlock();

int oldVertCount = _verticeCount;
_verticeCount *= (levels + 1);
if(device->CreateVertexBuffer((sizeof(LWVERTEX) * _verticeCount),
0,
0,
D3DPOOL_MANAGED,
&_vertexBuffer,
0) != D3D_OK)
ErrorMessenger::ReportMessage("Failed to create vertex buffer", __FILE__, __LINE__);

//Hold the new buffer info
LWVERTEX* newBufferInfo = new LWVERTEX[sizeof(LWVERTEX) * _verticeCount];

//For each triangle
for(int i = 0; i < oldVertCount; i += 3)
{

//Find hypotenuse
D3DXVECTOR3 hypoVec1;
D3DXVECTOR3 hypoVec2;
D3DXVECTOR3 oppHypotenuse = _FindHypotenuse(vertDataCopy[i].position, vertDataCopy[i + 1].position, vertDataCopy[i + 2].position, &hypoVec1, &hypoVec2);

//Find the midpoint on the hypotenuse line
D3DXVECTOR3 midPoint = D3DXVECTOR3(((hypoVec1.x + hypoVec2.x) / 2), ((hypoVec1.y + hypoVec2.y) / 2), ((hypoVec1.z + hypoVec2.z) / 2));

//New split triangle one
newBufferInfo[(i * 2)].position = oppHypotenuse;
newBufferInfo[(i * 2) + 1].position = hypoVec1;
newBufferInfo[(i * 2) + 2].position = midPoint;

//New split triangle two
newBufferInfo[(i * 2) + 3].position = oppHypotenuse;
newBufferInfo[(i * 2) + 4].position = hypoVec2;
newBufferInfo[(i * 2) + 5].position = midPoint;
}

//Set new vert data
void* newVertData;
_vertexBuffer->Lock(0, 0, &newVertData, 0);
memcpy(vertData, (void*)newBufferInfo, (sizeof(LWVERTEX) * _verticeCount));

//Exception occurs here
_vertexBuffer->Unlock();
}
[/code]

Share this post


Link to post
Share on other sites
Evil Steve    2017
For future reference, it's helpful to say exactly what the error you get is (E.g. "Access Violation reading 0x00000000"), since that can give a clue about the cause (E.g. an access violation reading an address near 0x00000000 means you probably tried to dereference a null pointer).

Share this post


Link to post
Share on other sites
LevyDee    376
[quote name='Evil Steve' timestamp='1329386501' post='4913609']
since that can give a clue about the cause (E.g. an access violation reading an address near 0x00000000 means you probably tried to dereference a null pointer).
[/quote]

I would like to think my debugging skills could find a simple problem like that =P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this