Help! Somebody! memory leak in my vertex buffer
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]
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
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
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?
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.
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.
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:
many times:
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
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
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.)
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.)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement