diff --git a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems
index ad0b58c0..7c78a112 100644
--- a/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems
+++ b/src/ComputeSharp.NetStandard/ComputeSharp.NetStandard.projitems
@@ -21,6 +21,7 @@
+
diff --git a/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/NativeMemory.cs b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/NativeMemory.cs
new file mode 100644
index 00000000..b5a23ff6
--- /dev/null
+++ b/src/ComputeSharp.NetStandard/System/Runtime/InteropServices/NativeMemory.cs
@@ -0,0 +1,35 @@
+namespace System.Runtime.InteropServices;
+
+///
+/// This class contains methods that are mainly used to manage native memory.
+///
+internal static unsafe class NativeMemory
+{
+ ///
+ /// Allocates a block of memory of the specified size, in bytes.
+ ///
+ /// The size, in bytes, of the block to allocate.
+ /// A pointer to the allocated block of memory.
+ /// Thrown when allocating of memory failed.
+ ///
+ /// This method allows to be 0 and will return a valid pointer that should not be dereferenced and that should be passed to free to avoid memory leaks.
+ /// This method is a thin wrapper over the C malloc API.
+ ///
+ public static void* Alloc(nuint byteCount)
+ {
+ return (void*)Marshal.AllocHGlobal(checked((int)byteCount));
+ }
+
+ ///
+ /// Frees a block of memory.
+ ///
+ /// A pointer to the block of memory that should be freed.
+ ///
+ /// This method does nothing if is .
+ /// This method is a thin wrapper over the C free API.
+ ///
+ public static void Free(void* ptr)
+ {
+ Marshal.FreeHGlobal((IntPtr)ptr);
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs
index 372c58f0..9e829f55 100644
--- a/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs
+++ b/src/ComputeSharp.NetStandard/System/Runtime/Versioning/SupportedOSVersionAttribute.cs
@@ -22,7 +22,6 @@
AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false)]
-
internal sealed class SupportedOSPlatformAttribute : OSPlatformAttribute
{
public SupportedOSPlatformAttribute(string platformName)
diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs
index 929fdef8..48845c10 100644
--- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs
+++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.ID3D12InfoQueue.cs
@@ -41,18 +41,23 @@ internal static partial class DeviceHelper
nuint length;
queue.Get()->GetMessage(i, null, &length);
- D3D12_MESSAGE* message = (D3D12_MESSAGE*)Marshal.AllocHGlobal((nint)length);
+ D3D12_MESSAGE* message = (D3D12_MESSAGE*)NativeMemory.Alloc(length);
- queue.Get()->GetMessage(i, message, &length);
+ try
+ {
+ queue.Get()->GetMessage(i, message, &length);
- builder.Clear();
- builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]");
- builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}");
- builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}");
- builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}");
- builder.Append($"[Description]: \"{new string(message->pDescription)}\"");
-
- Marshal.FreeHGlobal((IntPtr)message);
+ builder.Clear();
+ builder.AppendLine($"[D3D12 message #{i} for \"{device}\" (HW: {device.IsHardwareAccelerated}, UMA: {device.IsCacheCoherentUMA})]");
+ builder.AppendLine($"[Category]: {Enum.GetName(message->Category)}");
+ builder.AppendLine($"[Severity]: {Enum.GetName(message->Severity)}");
+ builder.AppendLine($"[ID]: {Enum.GetName(message->ID)}");
+ builder.Append($"[Description]: \"{new string(message->pDescription)}\"");
+ }
+ finally
+ {
+ NativeMemory.Free(message);
+ }
if (message->Severity is D3D12_MESSAGE_SEVERITY_ERROR or D3D12_MESSAGE_SEVERITY_CORRUPTION or D3D12_MESSAGE_SEVERITY_WARNING)
{
diff --git a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs
index c5ab0b64..efc66035 100644
--- a/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs
+++ b/src/ComputeSharp/Graphics/Helpers/DeviceHelper.IDXGIFactory6.cs
@@ -130,7 +130,7 @@ internal static partial class DeviceHelper
/// The resulting instance.
public static void Create(IDXGIFactory4* dxgiFactory4, IDXGIFactory6** dxgiFactory6)
{
- IDXGIFactory4As6Backcompat* @this = (IDXGIFactory4As6Backcompat*)Marshal.AllocHGlobal(sizeof(IDXGIFactory4As6Backcompat));
+ IDXGIFactory4As6Backcompat* @this = (IDXGIFactory4As6Backcompat*)NativeMemory.Alloc((nuint)sizeof(IDXGIFactory4As6Backcompat));
@this->lpVtbl = Vtbl;
@this->dxgiFactory4 = dxgiFactory4;
@@ -156,7 +156,7 @@ internal static partial class DeviceHelper
{
@this->dxgiFactory4->Release();
- Marshal.FreeHGlobal((IntPtr)@this);
+ NativeMemory.Free(@this);
return 0;
}