Sign in to follow this  
nPawn

Properly releasing LPD3DXMESH

Recommended Posts

nPawn    110
I just noticed in the DX9 examples that all they do to release a mesh (or invalidate it) is to do a SAFE_RELEASE( m_pLocalMesh ); call and that's it. I thought you would also need to do a Release or SAFE_RELEASE for all the model's materials and textures, or is that done automatically by releasing the mesh?

Share this post


Link to post
Share on other sites
circlesoft    1178
No, you also need to release all the textures, as well. The ID3DXMesh interface doesn't actually know anything about the textures, besides the filenames.

Here is what you should be doing:
(1) For each texture:
   (a) texture->Release();
(2) mesh->Release();

When I get home, I'll check out the SDK samples and find out exactly what they are doing.

Share this post


Link to post
Share on other sites
nPawn    110
Should I also delete or SAFE_DELETE the textures? or is that overkill or something that the release already did? And what about the D3DMATERIAL9's for the Mesh, does that need to be released and deleted?

Share this post


Link to post
Share on other sites
Uriel    115
Textures should be released, but not materials.
In general, when in doubt, check it out in sdk docs. If clas interface have Release() function, its is a sign that it would be good thing to release it.

Retriving lost device is a good way to find out if you release all that should be released...

Try to write releasing/retriving device code. Its good thing to do at the beggining. I did it when my project had over 10000 lines.. and it was hell.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Greetings!

When dealing with .X files you should release/delete:
- Hiererchy Frames
- Material buffers
- Textures
- Mesh itself

You may help yourself with _CrtOutputDebugString() it will tell you if your code is not released complete. If you have doubt's what to release, try FIRST SAFE_RELEASE( somethin' ) and if it fails, then go to SAFE_DELETE or SAFE_DELETE_ARRAY. Everything can be deleted, but not everything can be released. So there's the point. If you release someting that can be relesed it's done, if you can't release it, it's just some kind of buffer so delete it. Vioala. Pay attention with the 6th tutorial in MS DX Summer update SDK for Direct3D. There is it explained.

Share this post


Link to post
Share on other sites
circlesoft    1178
Quote:
Original post by nPawn
Should I also delete or SAFE_DELETE the textures?

Quote:
Original post by AP
If you have doubt's what to release, try FIRST SAFE_RELEASE( somethin' ) and if it fails, then go to SAFE_DELETE or SAFE_DELETE_ARRAY. Everything can be deleted, but not everything can be released.
It's simple - all DirectX interfaces are released - not deleted! Release() automatically deletes the objects. This actually originates from IUnknown and COM. For example, these interfaces need to be released (not deleted):
IDirect3D9
IDirect3D9
IDirect3DBaseTexture9
IDirect3DCubeTexture9
IDirect3DDevice9
IDirect3DIndexBuffer9
IDirect3DPixelShader9
IDirect3DQuery9
IDirect3DResource9
IDirect3DStateBlock9
IDirect3DSurface9
IDirect3DSwapChain9
IDirect3DTexture9
IDirect3DVertexBuffer9
IDirect3DVertexDeclaration9
IDirect3DVertexShader9
IDirect3DVolume9
IDirect3DVolumeTexture9
ID3DXAllocateHierarchy
ID3DXAnimationCallbackHandler
ID3DXAnimationController
ID3DXAnimationSet
ID3DXBaseEffect
ID3DXBaseMesh
ID3DXBuffer
ID3DXCompressedAnimationSet
ID3DXConstantTable
ID3DXEffect
ID3DXEffectCompiler
ID3DXEffectPool
ID3DXEffectStateManager
ID3DXFont
ID3DXFragmentLinker
ID3DXInclude
ID3DXKeyframedAnimationSet
ID3DXLine
ID3DXLoadUserData
ID3DXMatrixStack
ID3DXMesh
ID3DXPatchMesh
ID3DXPMesh
ID3DXRenderToEnvMap
ID3DXRenderToSurface
ID3DXSaveUserData
ID3DXSkinInfo
ID3DXSPMesh
ID3DXSprite

As you can see, this is all D3D and D3DX interfaces. If you get instantiate it from Direct3D or D3DX and it starts with an 'I' or 'LP', then it needs to be released.

Share this post


Link to post
Share on other sites
nPawn    110
Ok, one last question (hopefully =)

Some of these items like ID3DXFont and ID3DXSprite have an OnLostDevice function. Should I call that function when resetting a lost device, then Release the item (or in the opposite order?), or is OnLostDevice good enough?

Share this post


Link to post
Share on other sites
circlesoft    1178
When you have to reset the device, this is what you should do:
ResetDevice()
{
// On lost device comes first
ID3DXSprite::OnLostDevice()
ID3DXFont::OnLostDevice()
...

// Reset device
IDirect3DDevice9::Reset()

// On reset device comes after
ID3DXSprite::OnResetDevice()
ID3DXFont::OnResetDevice()
}

Share this post


Link to post
Share on other sites
nPawn    110
So let me get this straight for the special cases that have the OnLostDevice like D3DXSprite


device is lost (alt tab or something)
device is able to be reset now
pSprite->OnLostDevice()
device is reset()
pSprite->OnResetDevice()


No Release() call is needed for the pSprite in this case, and there's no need to reload the texture and stuff for it?

[Edited by - nPawn on July 7, 2004 4:05:19 PM]

Share this post


Link to post
Share on other sites
circlesoft    1178
Quote:
Original post by nPawn
device is lost (alt tab or something)
device is able to be reset now
pSprite->OnLostDevice()
device is reset()
pSprite->OnResetDevice()

That is correct.
Quote:
Original post by nPawn
No Release() call is needed for the pSprite in this case, and there's no need to reload the texture and stuff for it?

If the texture was created in the D3DPOOL_DEFAULT memory pool, then it must be reloaded. If it was created in D3DPOOL_MANAGED or D3DPOOL_SYSTEMMEM, then it doesn't have to be.

Share this post


Link to post
Share on other sites
nPawn    110
Thanks for the help, things are working now, had to figure out the view projection needed to be redone after a device reset.

[Edited by - nPawn on July 7, 2004 7:03:18 PM]

Share this post


Link to post
Share on other sites
circlesoft    1178
Sorry, I forgot to mention that. When the device is reset, all device states are lost. You have to reset all transforms, render states, and texture stage states. Also, you must reset all shaders.

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