Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

datadawgx

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

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
- 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 -

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!