Skip to content

Memory Leak #29

@Maklith

Description

@Maklith

Each screenshot operation causes an unmanaged memory leak
image

public static class ScreenCapture
{
    static DX11ScreenCaptureService screenCaptureService ;
    private static WeakReference _weakReference;
    static Queue<Bitmap> bitmaps = new();
    public static Queue<Bitmap> CaptureAllScreen()
    {
        screenCaptureService = new DX11ScreenCaptureService();
        foreach (var display in screenCaptureService.GetDisplays(screenCaptureService.GetGraphicsCards().First()))
        {
            var  screenCapture = screenCaptureService.GetScreenCapture(display);
            var fullscreen = screenCapture.RegisterCaptureZone(0, 0, screenCapture.Display.Width, screenCapture.Display.Height);
            while (!screenCapture.CaptureScreen()) { }
            SixLabors.ImageSharp.Configuration customConfig = SixLabors.ImageSharp.Configuration.Default.Clone();
            customConfig.PreferContiguousImageBuffers = true;
            using (fullscreen.Lock())
            {
              
            }
            _weakReference=new WeakReference(fullscreen);
        }

        return bitmaps;
    }
    public static WeakReference Dispose()
    {
        screenCaptureService.Dispose();
        screenCaptureService = null;
        return _weakReference;
    }
}

In Another Class:

 var captureAllScreen = CaptureAllScreen();
        var weakReference = Dispose();
        while (weakReference.IsAlive)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            Thread.Sleep(100);
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions