From e0ce62d2ec7bd7e106ca63179e603449acd325ae Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 23 Jul 2021 09:11:31 +0200 Subject: [PATCH] Prevent double allocations of memory block for quakeparms, and report if allocation of hunk memory fails. --- Assets/Scripts/QuakeParms.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Assets/Scripts/QuakeParms.cs b/Assets/Scripts/QuakeParms.cs index f983db9..7d5c845 100644 --- a/Assets/Scripts/QuakeParms.cs +++ b/Assets/Scripts/QuakeParms.cs @@ -29,7 +29,9 @@ public class QuakeParms public IntPtr ToNativePtr() { - nativeBlock = Marshal.AllocHGlobal(Marshal.SizeOf()); + if (nativeBlock == IntPtr.Zero) + nativeBlock = Marshal.AllocHGlobal(Marshal.SizeOf()); + Marshal.StructureToPtr(this, nativeBlock, false); return nativeBlock; } @@ -48,6 +50,10 @@ public class QuakeParms { memSize = memorySize; memBase = Marshal.AllocHGlobal(memorySize); + if (memBase == IntPtr.Zero) + { + throw new OutOfMemoryException($"Could not allocate {memorySize / 1024 / 1024} MB of heap memory!"); + } } public void Destroy()