Sign in to follow this  
DiPharoH

how to release this to avoid memory leak??

Recommended Posts

DiPharoH    100
i set my direct3d control panel to break at memory leaks so this line come in the output window Memory still allocated! Alloc count = 162 when i set the control panel to break at alloc id the program break at this line
[source lang = "cpp"]
 dw32BitFlag = ( pMesh->GetOptions() & D3DXMESH_32BIT );

but i release the pMesh like that
[source lang = "cpp"]
pMesh->Release();

if this is a false positive how can i override it???

Share this post


Link to post
Share on other sites
Evil Steve    2017
It's not a false positive, but I don't see how it could be breaking on that line. Are you sure it's not breaking on the line before / after?

This usually means that there's something holding onto the mesh pointer, you'll need to find out what it is. Usually, I just comment out bits of code until it goes away, then you know what's causing it.

Share this post


Link to post
Share on other sites
DiPharoH    100
[source lang = "cpp"]
LPD3DXBUFFER pD3DXMtrlBuffer = NULL;
D3DXMATERIAL* d3dxMaterials;
DWORD dw32BitFlag;
LPD3DXMESH pMesh = NULL;
LPD3DXPMESH pPMesh = NULL;
LPD3DXBUFFER pAdjacencyBuffer = NULL;
ID3DXPMesh** g_ppPMeshes = NULL;
ID3DXPMesh* g_pPMeshFull = NULL;
LPD3DXMESH pTempMesh;
DWORD g_cPMeshes = 0;
DWORD g_iPMeshCur;
D3DMATERIAL9* g_mtrlMeshMaterials = NULL;
LPDIRECT3DTEXTURE9* g_ppMeshTextures = NULL;// Array of textures, entries are NULL if no texture specified
DWORD g_dwNumMaterials = 0; // Number of materials
D3DXMATRIXA16 g_mWorldCenter;
D3DXVECTOR3 g_vObjectCenter; // Center of bounding sphere of object
FLOAT g_fObjectRadius;
//////////////////
void LoadPMesh1(void)
{


D3DXLoadMeshFromX("house.x", D3DXMESH_MANAGED, d3ddev,
&pAdjacencyBuffer, &pD3DXMtrlBuffer, NULL,
&g_dwNumMaterials, &pMesh);

dw32BitFlag = ( pMesh->GetOptions() & D3DXMESH_32BIT );

D3DXCleanMesh( D3DXCLEAN_SIMPLIFICATION, pMesh, ( DWORD* )pAdjacencyBuffer->GetBufferPointer(),
&pTempMesh,
( DWORD* )pAdjacencyBuffer->GetBufferPointer(), NULL );



pMesh->Release();
pMesh = pTempMesh;
D3DXWELDEPSILONS Epsilons;
ZeroMemory( &Epsilons, sizeof( D3DXWELDEPSILONS ) );
D3DXWeldVertices( pMesh, 0, &Epsilons,
( DWORD* )pAdjacencyBuffer->GetBufferPointer(),
( DWORD* )pAdjacencyBuffer->GetBufferPointer(), NULL, NULL );



D3DXValidMesh( pMesh, ( DWORD* )pAdjacencyBuffer->GetBufferPointer(), NULL );



d3dxMaterials= ( D3DXMATERIAL* )pD3DXMtrlBuffer->GetBufferPointer();
g_mtrlMeshMaterials = new D3DMATERIAL9[g_dwNumMaterials];
g_ppMeshTextures = new LPDIRECT3DTEXTURE9[g_dwNumMaterials];



for( UINT i = 0; i < g_dwNumMaterials; i++ )
{
g_mtrlMeshMaterials[i] = d3dxMaterials[i].MatD3D;
g_mtrlMeshMaterials[i].Ambient = g_mtrlMeshMaterials[i].Diffuse;

if( FAILED( D3DXCreateTextureFromFile( d3ddev,d3dxMaterials[i].pTextureFilename, &g_ppMeshTextures[i] ) ) )

{
g_ppMeshTextures[i] = NULL;
}
}




LPDIRECT3DVERTEXBUFFER9 pVertexBuffer = NULL;
void* pVertices;
pMesh->GetVertexBuffer( &pVertexBuffer );




pVertexBuffer->Lock( 0, 0, &pVertices, D3DLOCK_NOSYSLOCK );



D3DXComputeBoundingSphere( ( D3DXVECTOR3* )pVertices, pMesh->GetNumVertices(),
D3DXGetFVFVertexSize( pMesh->GetFVF() ),
&g_vObjectCenter, &g_fObjectRadius );



pVertexBuffer->Unlock();
pVertexBuffer->Release();

{
D3DXMatrixTranslation( &g_mWorldCenter, -g_vObjectCenter.x,
-g_vObjectCenter.y,
-g_vObjectCenter.z );
D3DXMATRIXA16 m;
D3DXMatrixScaling( &m, 2.0f / g_fObjectRadius,
2.0f / g_fObjectRadius,
2.0f / g_fObjectRadius );
D3DXMatrixMultiply( &g_mWorldCenter, &g_mWorldCenter, &m );
}




// If the mesh is missing normals, generate them.
if( !( pMesh->GetFVF() & D3DFVF_NORMAL ) )
{
pMesh->CloneMeshFVF( dw32BitFlag | D3DXMESH_MANAGED, pMesh->GetFVF() | D3DFVF_NORMAL,
d3ddev, &pTempMesh );

D3DXComputeNormals( pTempMesh, NULL );

pMesh->Release();
pMesh = pTempMesh;
}

// Generate progressive meshes

D3DXGeneratePMesh( pMesh, ( DWORD* )pAdjacencyBuffer->GetBufferPointer(),
NULL, NULL, 1, D3DXMESHSIMP_VERTEX, &pPMesh );

DWORD cVerticesMin = pPMesh->GetMinVertices();
DWORD cVerticesMax = pPMesh->GetMaxVertices();

DWORD cVerticesPerMesh = ( cVerticesMax - cVerticesMin + 10 ) / 10;

g_cPMeshes = max( 1, ( DWORD )ceil( ( cVerticesMax - cVerticesMin + 1 ) / ( float )cVerticesPerMesh ) );
g_ppPMeshes = new LPD3DXPMESH[g_cPMeshes];

ZeroMemory( g_ppPMeshes, sizeof( LPD3DXPMESH ) * g_cPMeshes );

// Clone full size pmesh
pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(), d3ddev, &g_pPMeshFull );


// Clone all the separate pmeshes
for( UINT iPMesh = 0; iPMesh < g_cPMeshes; iPMesh++ )
{
pPMesh->ClonePMeshFVF( D3DXMESH_MANAGED | D3DXMESH_VB_SHARE, pPMesh->GetFVF(),
d3ddev, &g_ppPMeshes[iPMesh] );


// Trim to appropriate space
g_ppPMeshes[iPMesh]->TrimByVertices( cVerticesMin + cVerticesPerMesh * iPMesh,
cVerticesMin + cVerticesPerMesh * ( iPMesh + 1 ), NULL, NULL );


g_ppPMeshes[iPMesh]->OptimizeBaseLOD( D3DXMESHOPT_VERTEXCACHE, NULL );

}

// Set current to be maximum number of vertices
g_iPMeshCur = g_cPMeshes - 1;
g_ppPMeshes[g_iPMeshCur]->SetNumVertices( cVerticesMax );
pD3DXMtrlBuffer->Release();
pAdjacencyBuffer->Release();
pAdjacencyBuffer = NULL;
pPMesh->Release();
pMesh->Release();


return;
}
///////////////
void SetNumVertices( DWORD dwNumVertices )
{


// If current pm valid for desired value, then set the number of vertices directly
if( ( dwNumVertices >= g_ppPMeshes[g_iPMeshCur]->GetMinVertices() ) &&
( dwNumVertices <= g_ppPMeshes[g_iPMeshCur]->GetMaxVertices() ) )
{
g_ppPMeshes[g_iPMeshCur]->SetNumVertices( dwNumVertices );
}
else // Search for the right one
{
g_iPMeshCur = g_cPMeshes - 1;

// Look for the correct "bin"
while( g_iPMeshCur > 0 )
{
// If number of vertices is less than current max then we found one to fit
if( dwNumVertices >= g_ppPMeshes[g_iPMeshCur]->GetMinVertices() )
break;

g_iPMeshCur -= 1;
}

// Set the vertices on the newly selected mesh
g_ppPMeshes[g_iPMeshCur]->SetNumVertices( dwNumVertices );
}
return;
}
////////
void DrawPMesh1(void)
{
for(DWORD i = 0; i < g_dwNumMaterials; i++ )
{
d3ddev->SetMaterial(&g_mtrlMeshMaterials[i]);
d3ddev->SetTexture(0,g_ppMeshTextures[i]);
g_ppPMeshes[g_iPMeshCur]->DrawSubset( i );
}
return;
}
//////////
void PMRelease(void)
{


for( UINT i = 0; i < g_dwNumMaterials; i++ )
{
if(g_ppMeshTextures[i] != NULL)
{
g_ppMeshTextures[i]->Release();
}
}
delete[] g_ppMeshTextures;
g_ppMeshTextures = NULL;
g_pPMeshFull->Release();
for( UINT i = 0; i < g_cPMeshes; i++ )
{
g_ppPMeshes[i]->Release();

g_cPMeshes = 0;
delete[] g_ppPMeshes;
g_ppPMeshes = NULL;
}
delete[] g_mtrlMeshMaterials;

g_dwNumMaterials = 0;

return;
}



[Edited by - DiPharoH on April 30, 2009 11:26:53 AM]

Share this post


Link to post
Share on other sites
Evil Steve    2017
It's probably the D3DXLoadMeshFromX() call that's causing the leak. All that means is that the mesh, or one of its buffers isn't being released.

By reading through that code, the following resources have not been released:
pAdjacencyBuffer, pMesh, pPMesh, g_pPMeshFull.

Share this post


Link to post
Share on other sites
DiPharoH    100
Quote:
Original post by Evil Steve
It's probably the D3DXLoadMeshFromX() call that's causing the leak. All that means is that the mesh, or one of its buffers isn't being released.

By reading through that code, the following resources have not been released:
pAdjacencyBuffer, pMesh, pPMesh, g_pPMeshFull.

i modified my code
please check it

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by DiPharoH
Quote:
Original post by Evil Steve
It's probably the D3DXLoadMeshFromX() call that's causing the leak. All that means is that the mesh, or one of its buffers isn't being released.

By reading through that code, the following resources have not been released:
pAdjacencyBuffer, pMesh, pPMesh, g_pPMeshFull.

i modified my code
please check it
pAdjacencyBuffer, pMesh and pPMesh are local variables, you can't access them from another function. You should be releasing them before the end of LoadPMesh1().

Share this post


Link to post
Share on other sites
DiPharoH    100
Quote:
Original post by Evil Steve
Quote:
Original post by DiPharoH
Quote:
Original post by Evil Steve
It's probably the D3DXLoadMeshFromX() call that's causing the leak. All that means is that the mesh, or one of its buffers isn't being released.

By reading through that code, the following resources have not been released:
pAdjacencyBuffer, pMesh, pPMesh, g_pPMeshFull.

i modified my code
please check it
pAdjacencyBuffer, pMesh and pPMesh are local variables, you can't access them from another function. You should be releasing them before the end of LoadPMesh1().

i modified my code again check it
but i still cannot get it
i am gonna get crazy
ohhhhhh no please help

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this