Wow, that is really nasty, you might have found a strange bug. Actually we normally do not dispose ContentData, they get cleaned up automatically. Can you maybe tell us how many times you called SetData and with how many pixels (width*height). I tried to reproduce it, but for me the memory did not increase significantly.
The maybe-bug might be in ContentData.cs
Code:
public void Dispose()
{
if (!IsDisposed)
DisposeData();
IsDisposed = true;
}
public bool IsDisposed { get; private set; }
protected abstract void DisposeData();
DisposeData for SharpDXImage should delete all resources just fine, but if it is not called because the IsDisposed flag was already true, there might be indeed a memory leak (in SharpDX only, maybe in SlimDX as well, but all other frameworks should use managed resources and not wrapped native pointers that might indeed leak):
Code:
protected override void DisposeData()
{
if (NativeTexture != null)
NativeTexture.Dispose();
if (NativeResourceView != null)
NativeResourceView.Dispose();
}
I have changed the code to reset the IsDisposed flag in the InternalLoad and InternalCreateDefault methods. However normally ContentData is not disposed and there is another issue in the SharpDXImage.Fill method, which always creates a new native texture and does not dispose the old data. I changed it to at least dispose the old data, which is not optimal. Better would be to overwrite the same memory like in the other frameworks, but I found no quick solution. You might be able to find a better solution looking around a bit in DirectX and SharpDX forums or questions on the internet. Let me know if you were able to improve that code:
Code:
public override void Fill(Color[] colors)
{
if (PixelSize.Width * PixelSize.Height != colors.Length)
throw new InvalidNumberOfColors(PixelSize);
if (NativeTexture != null)
DisposeData();
IsDisposed = false;
Utilities.Pin(colors, ptr =>
{
NativeTexture = new Texture2D(device.NativeDevice, CreateTextureDescription(),
new DataRectangle(ptr, (int)PixelSize.Width * 4));
});
SetSamplerState();
}
In any case you should probably not kill the image every single time and create a fresh image with new ImageCreationData every single time. As long as the size does not change (in which case you should really create a new image), just call Fill over and over again using the same resources. This will be much quicker (on all frameworks except SharpDX, where the code is not optimal yet, but will hopefully be soon much quicker) and you won't have any issues with Dispose.
Code:
public void SetData(PixelArray data)
{
Material.DiffuseMap.Fill(data.Array);
}
I am also quite unsure why you would create so many helper methods, seems like to only complicate things, try to just call the existing methods and your life will be much easier.
Edited by user Friday, February 21, 2014 4:55:27 PM(UTC)
| Reason: Not specified