Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in ImageElement #45

Closed
Guad opened this issue Aug 2, 2016 · 0 comments
Closed

Memory leak in ImageElement #45

Guad opened this issue Aug 2, 2016 · 0 comments

Comments

@Guad
Copy link

Guad commented Aug 2, 2016

Upon further inspection using a bigger image, recreating a new ImageElement every frame does indeed create a memory leak.

Some code.

The bitmap created on start up:
var bm = new Bitmap(/* path to big image*/);

The main loop:

using (Bitmap doubleBuffer = new Bitmap(ScreenSize.Width, ScreenSize.Height,
            PixelFormat.Format32bppArgb))
{
    using (var graphics = Graphics.FromImage(doubleBuffer))
    {
        graphics.DrawImage(bm, new Point(0, 0));
    }

    DirectXHook.SetBitmap(doubleBuffer);
}

Same results can be achieved by just calling SetBitmap without a doublebuffer.

DirectXHook.SetBitmap(bm);

SetBitmap method, where this.OverlayEngine.Overlays[0].Elements[1] is an ImageElement

((ImageElement)this.OverlayEngine.Overlays[0].Elements[1]).Dispose();
this.OverlayEngine.Overlays[0].Elements.RemoveAt(1);
this.OverlayEngine.Overlays[0].Elements.Add(new Common.ImageElement(bt, true)
{
    Location = new System.Drawing.Point(0, 0)
});

this.OverlayEngine.FlushCache();

Finally, FlushCache method

public void FlushCache()
{
    lock (_imageCache)
    {
        foreach (var dxImage in _imageCache)
        {
            dxImage.Value?.Dispose();
            ((ImageElement)dxImage.Key).Dispose();
        }

        foreach (var element in Overlays[0].Elements.OfType<ImageElement>())
        {
            DXImage result = ToDispose(new DXImage(_device, _deviceContext));
            result.Initialise(element.Bitmap);
            _imageCache[element] = result;
        }
    }
}

SharpDX.ObjectTracker output

Count per Type:
BlendState : 1
Buffer : 2
Device : 2
DeviceContext : 3
Effect : 1
InputLayout : 1
RenderTargetView : 1
ShaderResourceView : 2
SwapChain : 28
Texture2D : 50

Texture2D never goes above 50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant