VertexBuffer management in 2D Swing-like GUI framework- memory leaks?

Started by
2 comments, last by datadawgx 20 years ago
ok so here''s yet another vertex buffer question. im creating my own little framework of swing-like DirectX 8 classes that i can use in my games for menus, buttons, layouts, etc. so far, its going great. i got my uml designs laid out, using composite and command patterns quite a bit, but one of my most fundamental classes is DXLine, a class that is renderable (it extends DXRenderable, an abstract class) and basically uses a DXRenderable''s state to create a line. it works fine, except i got one issue. If a DXLine is part of another component, like a DXWindow, then it uses DXWindow''s vertex buffer to render itself (they would be of same size). However, if DXLine is instantiated by itself without being a member of a window or shape or whatever, it uses CreateVertexBuffer to create its own Vertex Buffer. This works... except im stress testing it by creating lines and deleting them as fast as possible in my GameRender loop, and im getting memory leaks that lead to crashes in about 10 mins on a 512MB machine. Yes, in the DXLine''s deconstructor i call pVB->Release() and then delete pVB, but to no effect!! Somehow, i have a memory leak. So my code is below - maybe someone can point out something im missing. One possible solution im thinking of doing is creating a single static vertex buffer for all lines and forcing them all to use that buffer to render with. itll prob solve this problem, but im affraid itll couple my design up. any suggestions?


class DXRenderable
{
public:
	virtual void Render() = 0;
	virtual void SetPosition(float, float, float) = 0;
	virtual void SetSize(float, float) = 0;
	
	
protected:
	bool VBOwner; //does this object have its own vertex buffer?

	float Width;
	float Height;
	float xPos, yPos;
	D3DXMATRIX translationMatrix;
	LPDIRECT3DVERTEXBUFFER8 pVB;
	IDirect3DDevice8* pID3DDevice;
};

class DXLine : public DXRenderable {
	
public:
	struct EDGEVERTEX
	{
		FLOAT x, y, z;   // The untransformed position for the vertex.

		DWORD color;
	};

	DXLine( IDirect3DDevice8* dev );
	DXLine( LPDIRECT3DVERTEXBUFFER8 buf, IDirect3DDevice8* dev );
	~DXLine();
	void SetPosition(float, float, float);
	void SetSize(float, float);
	void Render();
private:
	EDGEVERTEX vertArray[2];
};

//creates its own vertex buffer

DXLine::DXLine(	IDirect3DDevice8* dev ){
	pID3DDevice = dev;

//THIS IS WHERE THE PROBLEM IS	

pID3DDevice->CreateVertexBuffer(2*sizeof(EDGEVERTEX),//Size of memory to be allocated

		//Number of vertices * size of a vertex

		D3DUSAGE_WRITEONLY,  //We never need to read from it so

		//we specify write only, it''s faster

		(D3DFVF_XYZ | D3DFVF_DIFFUSE),  //Our custom vertex specifier (coordinates & a colour)

		D3DPOOL_MANAGED,     //Tell DirectX to manage the memory of this resource

		&pVB);       //Pointer to our Vertex Buffer, after this call

	VBOwner = true;
	xPos = 0.0f;
	yPos = 0.0f;
	Width = 0.0f;
	Height = 0.0f;

	D3DXMatrixTranslation(&translationMatrix,0.0f,0.0f,0.0f);
}

//uses a "parent object"''s vertex buffer. saves space & time

DXLine::DXLine(	LPDIRECT3DVERTEXBUFFER8 buf, IDirect3DDevice8* dev ){
	pID3DDevice = dev;
	pVB = buf;
	VBOwner = false;
	xPos = 0.0f;
	yPos = 0.0f;
	Width = 0.0f;
	Height = 0.0f;

	D3DXMatrixTranslation(&translationMatrix,0.0f,0.0f,0.0f);
}

DXLine::~DXLine(){
	
	if (VBOwner) {
                pVB->Release();
		delete pVB;
	}
	delete translationMatrix;
}
thanks guys
Advertisement
because of the Quarks of reference counting, it seems as though you are not putting the ref count at 0 before you null your pointer. Im not entirely sure why this always happens or the best way to solve it but, I solve it by counting the references left on a pointer. ie Release() returns an int. that int is how many refs are left. Im really not sure exactly who told me about this but it fixed my memory leaks, kindof dirty though seeing as how ive got things getting referenced all over the place and i dont even know about it. If this doesnt solve your problem i guess you have one less thing to test.
- You should never delete anything that wasn''t created with new:
a) Don''t delete COM pointers. When you Release() a COM interface pointer, and the reference count reaches zero, Release() will delete the object itself.
b) Don''t delete translationMatrix, because it''s not allocated with new, it''s not even a pointer.

- You should AddRef the device as long as you keep a local copy

Muhammad Haggag,
Optimize
Bitwise account: MHaggag -

good ideas.. ill try em out first thing tomorrow.

This topic is closed to new replies.

Advertisement