Jump to content
  • Advertisement
Sign in to follow this  
dave

SlimDX: Device.VertexDeclaration

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

Evening,

My C# app is running out of memory and after hunting it down i discovered that it was where i was doing the following:

			SlimDX.Direct3D9.VertexDeclaration Declaration = new SlimDX.Direct3D9.VertexDeclaration(Render.Device, Elements);

SlimDX.Matrix WVP;

WVP = SlimDX.Matrix.Identity;
WVP = SlimDX.Matrix.Multiply(WVP, m_Transform);
WVP = SlimDX.Matrix.Multiply(WVP, Render.Camera.ViewTransform);
WVP = SlimDX.Matrix.Multiply(WVP, Render.Camera.ProjectionTransform);

Effect.SetValue(new SlimDX.Direct3D9.EffectHandle("matWorldViewProjection"), WVP.ToArray());
Effect.SetValue(new SlimDX.Direct3D9.EffectHandle("bOverrideColour"), m_bOverrideColour);
Effect.SetValue(new SlimDX.Direct3D9.EffectHandle("OverrideColour"), m_OverrideColour);
Effect.CommitChanges();
Render.Device.VertexDeclaration = Declaration;
Render.Device.DrawUserPrimitives(SlimDX.Direct3D9.PrimitiveType.LineList, 1, Verts);

Declaration.Dispose();


I know that creating vertex declarations on the fly is not wise, this is temporary code. Now, the leak occurs when i don't manually Dispose() of the vertex declaration.

I bring this up for 3 reasons:

1) I am now a bit confused about when i should Dispose() manually and when i shouldn't. My impression was that if i know i dont need it any more and want it cleaned up, i Dispose() it. Otherwise the GC does it. Is this correct?

2) I thought there is a chance that this is a bug in SlimDX, though i doubt it.

3) There is a documentation error for Direct3D9.Device.VertexDeclaration. It states that a Result is returned when it's a property.

Thanks guys,

Share this post


Link to post
Share on other sites
Advertisement
Quote:
...Otherwise the GC does it...


In this very case, no, since SlimDX does not provide finalizers for their ComObjects, (i.e. the wrappers for the underlying DirectX-object). There's an entry in the documentation "Object Lifetimes" explaining this. So, yes, make sure you call Dispose() for temporaries or stuff you don't need anymore.

If you ever forget to dispose manually, it should show up in the DirectX debug output (if enabled).

unbird

Share this post


Link to post
Share on other sites
The short answer is that finalization occurs in a dedicated thread and D3D does not approve of such things.

Share this post


Link to post
Share on other sites
As jpetrie said, Direct3D (9 at least) requires that objects are released on the same thread they were created. The finalizer is run on a separate thread at some unspecified time, so the two don't play well. Even if we had provided finalizers, it'd still be a better idea to manually call Dispose yourself. Objects that have a finalizer add extra strain on the garbage collector and their collection can end up being delayed and promoted into the next generation, which is something you don't want to happen to temporaries in your main loop.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!