Click to See Complete Forum and Search --> : Releaseing DirectX - memory leaks
^Johnny2Bad
July 29th, 2005, 05:03 PM
Hi,
I am having continual memory leaks from my directx code and I believe it is because of the way I am releasing the directx objects. Here is the code I use to release the memory...
void CStarMapDlg::OnClose()
{
for (int i=0; i<100; i++)
Stars[i].Destroy; //mesh class with release functions
//for meshes
D3Ddevice->Release();
D3DObj->Release();
CDialog::OnClose();
}
Does anybody know what I am doing wrong?
sunn
July 29th, 2005, 10:24 PM
did u REALLY know or be REAALY :)sure that it is a hadle that has memory
looks like u release a thing that actully didnt even KNOW if it exists there for ya :( or for some1 :(
if this piece of hint helpfull, rate it ;) nice day !
^Johnny2Bad
July 30th, 2005, 03:45 PM
Maybe I should've been more descriptive in my post
D3Ddevice is a handle to the directx device
D3DObj is a handle to the directx object
The Mesh objects are as I explained in the code.
I don't know how I can describe it more simpler.
philkr
July 31st, 2005, 11:12 AM
1) You forgot the brackets after 'Destroy'
2) To avoid errors it is good programming habit to 'safe release' like this:
if(Object) { Object->Release(); Object = NULL; }
3) The memory leak can be because you forgot to release something in your Destroy function or anywhere else. So you have to post more code.
^Johnny2Bad
August 1st, 2005, 03:02 AM
I did what you suggested philkr with the D3Ddevice and object, but I still have a huge amount of memory leaks. So I believe you are correct in saying that the Destroy function is causing the problems. I find it hard to believe that the code has errors in it as the Mesh class is derived from a microsoft one that came with a directx application builder in an earlier version of directx 9.0. Anyway here's the code..
void CMesh::Destroy(void)
{
delete[] m_Materials;
for (DWORD i=0; i<m_NumberOfMaterials; i++)
{
m_Textures[i]->Release();
}
delete[] m_Textures;
m_Mesh->Release();
}
Where m_Mesh is decleared as a LPD3DXMESH. m_Materials is D3DMATERIAL9* and m_Textures is LPDIRECT3DTEXTURE9*.
Can anyone see anything wrong with this method?
philkr
August 1st, 2005, 03:24 AM
You are realising m_NumberOfMaterials number of textures. I don't know your design, so perhaps you are right with that, but in general not every material must have a texture, or there are some materials with the same texture but different reflexion values. Unless you have coded it in a way that every material has a different texture, the assumption is that m_NumberOfMaterial is not always m_NumberOfTextures.
Besides this I can't see anything wrong, but maybe something is missing.
^Johnny2Bad
August 2nd, 2005, 04:04 AM
The construction of the CMesh object is done through a .x file loader. Here is the following code.....
void CMesh::Create(LPDIRECT3DDEVICE9 m_Device, CString m_filename)
{
LPD3DXBUFFER pD3DXMtrlBuffer;
// load the mesh
D3DXLoadMeshFromX(m_filename,D3DXMESH_SYSTEMMEM,m_Device,NULL,&pD3DXMtrlBuffer,NULL,&m_NumberOfMaterials,&m_Mesh);
//extract material and texture properties from mesh
D3DXMATERIAL* d3dxMaterials=(D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();
m_Materials=new D3DMATERIAL9[m_NumberOfMaterials];
m_Textures=new LPDIRECT3DTEXTURE9[m_NumberOfMaterials];
for (DWORD i=0; i<m_NumberOfMaterials; i++)
{
//copy the material
m_Materials[i]=d3dxMaterials[i].MatD3D;
// set the ambient colour for the material
m_Materials[i].Ambient=m_Materials[i].Diffuse;
m_Textures[i]=NULL;
//Create the texture
D3DXCreateTextureFromFile(m_Device,d3dxMaterials[i].pTextureFilename,&m_Textures[i]);
}
pD3DXMtrlBuffer->Release();
}
Can anyone see where the memory leaks are occuring now?
Cheers,
Jonathan.
Mike Harnad
August 2nd, 2005, 05:21 PM
DirectX installs with a control panel applet. Try enabling the debugging switches on the Direct3D page. Make sure to turn on the "break on memory leaks" flag.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.