Help! Somebody! memory leak in my vertex buffer

Started by
5 comments, last by Christian Schlager 21 years, 2 months ago
I dont know what to do. The debugger tells me i have a memory leak in my directx program: Direct3D9: (ERROR) :Memory still allocated! Alloc count = 796 Direct3D9: (ERROR) :Current Process (pid) = 00000a48 ..yadda yadda..memory adresses Direct3D9: (ERROR) :Total Memory Unfreed From Current Process = 55056 bytes When the program runs for several minutes, i get a CreateVertexBuffer() error. This is the code of the draw function. (it draws a select box): CUSTOMVERTEX vertices[5]; vertices[0].position = D3DXVECTOR3(select.x, select.y, 0.0); vertices[1].position = D3DXVECTOR3(cursor.x, select.y, 0.0); vertices[2].position = D3DXVECTOR3(cursor.x, cursor.y, 0.0); vertices[3].position = D3DXVECTOR3(select.x, cursor.y, 0.0); vertices[4].position = D3DXVECTOR3(select.x, select.y, 0.0); for (DWORD i = 0; i < 5; i++) vertices.color = D3DCOLOR_XRGB(255, 0, 0); if (FAILED(d3ddevice->CreateVertexBuffer(5 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &vb, NULL))) message("CreateVertexBuffer failed"); VOID* pVertices; if(FAILED(vb->Lock(0, sizeof(vertices), (void**)&pVertices, 0))) message("Lock failed"); memcpy(pVertices, vertices, sizeof(vertices)); vb->Unlock(); d3ddevice->SetStreamSource(0, vb, 0, sizeof(CUSTOMVERTEX)); d3ddevice->SetFVF(D3DFVF_CUSTOMVERTEX); d3ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 4); Any help would be nice, since im really desperate. [edited by - Christian Schlager on February 9, 2003 7:00:32 PM]
Advertisement
First of all I would suggest creating your vertex buffer only once at startup instead of every time you draw.

Sounds like the problem is that you are creating a vertex buffer every time you call draw but never releasing it. Since I didn''t see a call to Release for your vertex buffer in there I assume this is what is happenening.

For now call Release on the vertex buffer at the end of your drawing call. You really should change this to only create the vertex buffer at startup and release it at shutdown.

-dizzy
-dizzyGame Institute InternPlease rate me. :)
i dont quite understand.
Im dragging the select box and
the position of the vertices
changes. So i have to update the
vertices and therefore build the
vertex buffer new. or am i getting
this totally wrong?
Create your VB outside the loop with D3DUSAGE_DYNAMIC|D3DUSAGE_WRITEONLY,

Fill it in the loop with the vertices. when you change the vertices, relock it and feed it again with offset 0 and size, the size of ur vertex type. For lock parameters, use D3DLOCK_DISCARD to discard the entire vb contents.

Release VB before releasing device and d3d objects. Render as you do.
GraphicsWare|RenderTechhttp://www.graphicsware.com3D Graphics & Solutions
you don''t need to create the buffer everytime you want to change the vertices, just create it at the begining, when you want to change the vertices lock''em like you are doing now

1 time:

  if (FAILED(d3ddevice->CreateVertexBuffer(5 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &vb, NULL)))message("CreateVertexBuffer failed");CUSTOMVERTEX vertices[5];for (DWORD i = 0; i < 5; i++)vertices.color = D3DCOLOR_XRGB(255, 0, 0);  


many times:

  vertices[0].position = D3DXVECTOR3(select.x, select.y, 0.0);vertices[1].position = D3DXVECTOR3(cursor.x, select.y, 0.0);vertices[2].position = D3DXVECTOR3(cursor.x, cursor.y, 0.0);vertices[3].position = D3DXVECTOR3(select.x, cursor.y, 0.0);vertices[4].position = D3DXVECTOR3(select.x, select.y, 0.0);VOID* pVertices;if(FAILED(vb->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))message("Lock failed");memcpy(pVertices, vertices, sizeof(vertices));vb->Unlock();d3ddevice->SetStreamSource(0, vb, 0, sizeof(CUSTOMVERTEX));d3ddevice->SetFVF(D3DFVF_CUSTOMVERTEX);d3ddevice->DrawPrimitive(D3DPT_LINESTRIP, 0, 4);  


a then finish

vb->Release();


hope it helps







To be considered a genius you just have to say what everybody knows in a way very few understand
To be considered a genius you just have to say what everybody knows in a way very few understand
Thanks guys!

i did what you suggested and i got that stupid alloc count down to 340. So its kind of a success! (at least if i knew what that alloc count is.)
ah crap, i also forgot to release the textures....
woohoo, no more memory leaks!
thanks, this forum is great!

This topic is closed to new replies.

Advertisement