Skip to content

Commit 088a763

Browse files
committed
fix(Core): release resources in Windows CaptureScreen
1 parent 7d4abdd commit 088a763

1 file changed

Lines changed: 25 additions & 10 deletions

File tree

SnapX.Core/SharpCapture/Windows/WindowsCapture.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ private static ID3D11Texture2D Texture2DFromSurface(IDirect3DSurface surface)
242242

243243
var width = size.Width;
244244
var height = size.Height;
245-
var currentFrame = Texture2DFromSurface(result.Surface);
246-
var tempTexture = currentFrame.QueryInterface<ID3D11Texture2D>();
245+
using var currentFrame = Texture2DFromSurface(result.Surface);
246+
using var tempTexture = currentFrame.QueryInterface<ID3D11Texture2D>();
247247

248248
d3d11Device.ImmediateContext.CopyResource(currentFrame, tempTexture);
249249
var dataBox = d3d11Device.ImmediateContext.Map(currentFrame, 0);
@@ -315,9 +315,16 @@ private List<IDXGIAdapter1> EnumerateAdapters(IDXGIFactory1 factory)
315315
if (adapter == null) throw new ArgumentNullException(nameof(adapter));
316316
if (bounds.Width <= 0 || bounds.Height <= 0) throw new ArgumentException("Invalid bounds", nameof(bounds));
317317
var hr = D3D11.D3D11CreateDevice(adapter, DriverType.Unknown, DeviceCreationFlags.BgraSupport,
318-
[FeatureLevel.Level_11_1], out var device);
319-
if (hr.Failure) throw new InvalidOperationException($"D3D11CreateDevice failed: {hr}");
320-
output.GetParent<IDXGIAdapter>(out var outputAdapter);
318+
[FeatureLevel.Level_11_1], out var d3dDevice );
319+
using var device = d3dDevice;
320+
if (hr.Failure) throw new InvalidOperationException(
321+
$"D3D11CreateDevice failed: {hr} ({hr.Description}) " +
322+
$"Module: {hr.Module}, API: {hr.ApiCode}, Native: {hr.NativeApiCode}"
323+
);
324+
if (device == null) throw new InvalidOperationException("D3D11CreateDevice failed, out device is NULL");
325+
output.GetParent<IDXGIAdapter>(out var outAdapter);
326+
using var outputAdapter = outAdapter;
327+
if (outputAdapter == null) throw new ArgumentNullException(nameof(outputAdapter));
321328
if (!outputAdapter?.NativePointer.Equals(adapter.NativePointer) ?? false)
322329
{
323330
throw new InvalidOperationException("The IDXGIAdapter used does not match the one used to create this IDXGIOutput1.");
@@ -343,9 +350,16 @@ private List<IDXGIAdapter1> EnumerateAdapters(IDXGIFactory1 factory)
343350
DebugHelper.WriteLine($"DedicatedVideoMemory : {desc.DedicatedVideoMemory / 1024 / 1024} MB");
344351
DebugHelper.WriteLine($"DedicatedSystemMemory : {desc.DedicatedSystemMemory / 1024 / 1024} MB");
345352
DebugHelper.WriteLine($"SharedSystemMemory : {desc.SharedSystemMemory / 1024 / 1024} MB");
353+
DebugHelper.WriteLine("=== Device Info ===");
354+
DebugHelper.WriteLine($"DebugName : {device.DebugName ?? "N/A"}");
355+
DebugHelper.WriteLine(
356+
$"DeviceRemovedReason : {(device.DeviceRemovedReason.Success ? $"{device.DeviceRemovedReason.Description} ({device.DeviceRemovedReason.ApiCode}, {device.DeviceRemovedReason.Module}) " : "")}"
357+
);
358+
DebugHelper.WriteLine($"CreationFlags : {device.CreationFlags}");
359+
346360
DebugHelper.WriteLine("======================");
347361

348-
var duplication = output.DuplicateOutput(device);
362+
using var duplication = output.DuplicateOutput(device);
349363

350364
var textureDesc = new Texture2DDescription
351365
{
@@ -360,12 +374,13 @@ private List<IDXGIAdapter1> EnumerateAdapters(IDXGIFactory1 factory)
360374
SampleDescription = { Count = 1, Quality = 0 },
361375
Usage = ResourceUsage.Staging
362376
};
363-
var currentFrame = device.CreateTexture2D(textureDesc);
377+
using var currentFrame = device.CreateTexture2D(textureDesc);
364378

365-
await Task.Delay(100);
379+
await Task.Delay(150);
366380

367-
duplication.AcquireNextFrame(500, out var frameInfo, out var desktopResource);
368-
var tempTexture = desktopResource.QueryInterface<ID3D11Texture2D>();
381+
duplication.AcquireNextFrame(500, out var frameInfo, out var dskTopResource);
382+
using var desktopResource = dskTopResource;
383+
using var tempTexture = desktopResource.QueryInterface<ID3D11Texture2D>();
369384

370385
device.ImmediateContext.CopyResource(currentFrame, tempTexture);
371386
var dataBox = device.ImmediateContext.Map(currentFrame, 0);

0 commit comments

Comments
 (0)