# 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.

## 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 on other sites
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 on other sites
OK great, thanks.

Why do they not specify finalizers then?

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

##### 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.

• 47
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631374
• Total Posts
2999647
×