It is probably incorrect to call Dispose from ~DX( ). Maybe you should implement the IDisposable interface according to Visual Studio's suggestions:
public class DX : IDisposable
{
. . .
private bool disposedValue;
protected virtual void Dispose( bool disposing )
{
if( !disposedValue )
{
if( disposing )
{
// TODO: dispose managed state (managed objects)
device?.Dispose( );
d3d?.Dispose( );
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
// TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~DX( )
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose( disposing: false );
}
public void Dispose( )
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose( disposing: true );
GC.SuppressFinalize( this );
}
}
Usage:
public void DX9Test( )
{
using( var dx9 = new DX( ) )
{
}
}
The leaks seem to be much smaller. See a similar issue:
Probably the real code will not create such multiple objects, or the internal memory will be released after normal usage of objects.