When I try to use ->Release() on vertex buffers that have been moved, game crashes.

Started by
4 comments, last by Megatron 21 years, 11 months ago
My problem is this. When I create a vertex buffer, transformed or untransformed, and then move it, I am unable to release the vertex buffer associated with it later on unless the original coordinates have been restored. So if my vertex buffer starts at 0,0 and I move it 5 to the right, when I call myBuffer->Release() on it, the game crashes unless i move it back to 0,0 just before I clean up. This seems ridiculous to have to do with every thing that moves on the screen. Has anyone else encountered this problem or found a solution?
Advertisement
How do you move a vertex buffer? Do you mean translate a vertex in the buffer or what?

Anyway, use ATL''s CComPtr template wrapper to manage COM interfaces. Never call Release or write SAFERELEASE macros again. It''s in the file <atlbase.h> (if you don''t have it, you can download it from MSDN).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
explain in more detail. are you sure you locked the buffer with write privilages? you did not overflow anything did you? what is the error returned from lock() or unlock() if any? error code for release()? what are the flags used to create the buffer? you are NOT touching the object pointer by changing its value. its NOT meant to be used like that. a vertex buffer is an object that when locked, will give you the address to the vertex data through an address of a pointer you pass to lock(). read the sdk docs. it will become clear.
My move function translates the vertices. As far as the vertex buffer goes, I''m talking about the IDirect3DVertexBuffer8* that I pass into the create vertex buffer function. So in the top of my code, I do something like

//Make a vb pointer
IDirect3DVertexBuffer8* buffer1 = NULL;


Then I pass buffer1 into CreateVertexBuffer(). Then, when I want to translate my vertices, I pass a pointer to the transformed vertex array and do my alterations...
move(T_VERTEX* vertex, IDirect3DVertexBuffer8* buffer)
{
.
.
vertex[0].x ++;
vertex[1].x ++;

etc.

hr=buffer->Lock(0,0, &vb_vertices,0);
memcpy(vb_vertices, vertex, sizeof(T_VERTEX)*4 );
buffer->Unlock();

}


But later on, when I am cleaning house, I say

if (buffer1)
{
buffer1->Release();
buffer1=NULL;
}

This will cause my game to crash if I haven''t moved the vertex back to where it was to begin with when I first assigned its coordinates. So I''d have to go

vertex[0].x --;
vertex[1].x --;

etc.

in this case to make things work.
vertex points to sizeof(T_VERTEX)*4 amount of data right? and your vertex buffer as that much space allocated correct? also what does the lock() return? you dont seem to be placing the error code there. also is your vertex buffer dynamic? it should be. you should also be using the flag D3DLOCK_DISCARD when locking.

your vertex* point to memory you allocated, and not something dx allocated. you are not allowed to read from vertex buffers created with the write only flag.

dx dont transform vertices till its render time (ie call to drawprim). you dont get access to them unless you use other functions to transform the data first. post your vertex buffer creation code like i asked before, and the return code (what the code means not the number) for the lock and creation of the buffer. you are not checking whether the lock is even sucessful. VERY bad.
Here is my vertex buffer creation code.


    IDirect3DVertexBuffer8* FVF::CreateFVFBuffer(T_VERTEX* vertex, int numberOfVertices) {IDirect3DVertexBuffer8* buffer = NULL;hr=D3D_Device->CreateVertexBuffer( numberOfVertices*sizeof( T_VERTEX),D3DUSAGE_WRITEONLY,D3D8T_CUSTOMVERTEX,D3DPOOL_MANAGED,&buffer);                                                                      if(FAILED(hr))   {      //FatalError(hr,"Error creating vertex buffer");   }      hr=buffer->Lock(0,0,&vb_vertices,0);		   if(FAILED(hr))   {      //FatalError(hr,"Error Locking buffer");   }      memcpy(vb_vertices, vertex, sizeof(T_VERTEX)* numberOfVertices );   buffer->Unlock();   return buffer; }//end FVF::CreateFVFBuffer()    


And here is how I am calling it.

groundBuffer = FVF::CreateFVFBuffer(GroundVertices,4);

Also, here is how I'm trying to clean it up


  void FVF::KillFVFBuffer(IDirect3DVertexBuffer8* &buffer){	if (buffer)	{		buffer->Release();		buffer = NULL;	}}//end FVF::KillFVFBuffer();  


[edited by - Megatron on May 1, 2002 4:39:05 PM]

This topic is closed to new replies.

Advertisement