Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

Shallow Copying of the pMeshData in CreateMeshContainer

This topic is 2297 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

When I make a shallow copy of pMeshData to OriginalMesh (for static mesh rendering, 2 objects pointing to one COM interface, memory leaks start to occur.
When I don't, everything is fine.
How to solve it? I addref() to it once (with pMeshData)
and ReleaseCOM it once in destroyMeshContainer
Thanks
Jack Edited by lucky6969b

Share this post


Link to post
Share on other sites
Advertisement

HRESULT CAllocMeshHierarchy::CreateMeshContainer(PCTSTR Name,
const D3DXMESHDATA* pMeshData, const D3DXMATERIAL* pMaterials,
const D3DXEFFECTINSTANCE* pEffectInstances, DWORD NumMaterials,
const DWORD *pAdjacency, ID3DXSkinInfo* pSkinInfo,
D3DXMESHCONTAINER** ppNewMeshContainer)
{
// Remark: CreateMeshContainer should always return a non-null value
// for ppNewMeshContainer, even if we are not interested in the mesh
// (i.e., it contains no skin info), otherwise D3DXLoadMeshHierarchyFromX
// will interpret it as a failed operation.

//===============================================================
// Allocate a new D3DXMESHCONTAINER, and set its name.
MeshContainer* meshContainer = new MeshContainer();
::ZeroMemory(meshContainer, sizeof(D3DXMESHCONTAINER));
if( Name ) CopyString(Name, &meshContainer->Name);
else CopyString("<no name>", &meshContainer->Name);

//===============================================================
// Copy material data, and allocate memory for texture file names.
meshContainer->NumMaterials = NumMaterials;
meshContainer->pMaterials = new D3DXMATERIAL[NumMaterials];
for(DWORD i = 0; i < NumMaterials; ++i)
{
D3DXMATERIAL* mtrls = meshContainer->pMaterials;
mtrls.MatD3D = pMaterials.MatD3D;
mtrls.MatD3D.Ambient = pMaterials.MatD3D.Diffuse;
meshContainer->materials.push_back(mtrls.MatD3D);
CopyString(pMaterials.pTextureFilename,
&mtrls.pTextureFilename);
}
//===============================================================
// Ignore effect instances and adjacency info for this demo.
meshContainer->pEffects = 0;
meshContainer->pAdjacency = 0;

//===============================================================
// Save mesh and skininfo.
meshContainer->MeshData.Type = D3DXMESHTYPE_MESH;
meshContainer->MeshData.pMesh = pMeshData->pMesh;
meshContainer->OriginalMesh = pMeshData->pMesh;
meshContainer->pSkinInfo = pSkinInfo;
pMeshData->pMesh->AddRef();
if (pSkinInfo)
pSkinInfo->AddRef();



//===============================================================
// Save our created mesh container now because we might return
// early, and we must _always_ return an allocated container.
*ppNewMeshContainer = meshContainer;
return D3D_OK;
}


HRESULT CAllocMeshHierarchy::DestroyMeshContainer(D3DXMESHCONTAINER* pMeshContainer)
{
MeshContainer *pMeshContainerBase = (MeshContainer*) pMeshContainer;
delete[] pMeshContainerBase->Name;
delete[] pMeshContainerBase->pAdjacency;
delete[] pMeshContainerBase->pEffects;

for(DWORD i = 0; i < pMeshContainerBase->NumMaterials; ++i)
delete[] pMeshContainerBase->pMaterials.pTextureFilename;
delete[] pMeshContainerBase->pMaterials;
ReleaseCOM(pMeshContainerBase->MeshData.pMesh);
ReleaseCOM(pMeshContainerBase->OriginalMesh);

ReleaseCOM(pMeshContainerBase->pSkinInfo);

delete pMeshContainerBase;

return D3D_OK;
}

If you mean to Release a COM object twice, I am afraid that doesn't work.

MeshData.pMesh is 0x00000000 while OriginalMesh is a pointer of some value whose values are 0xfeeefeee

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!