From bb3435550f9aa0b6ab9296bde7704de2f3840e04 Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 29 Oct 2019 10:44:02 -0600 Subject: [PATCH] Added SafeAllocatedMemoryHandle.TakeOwnership method to take control of the allocated memory. --- Core/InteropServices/SafeMemoryHandle.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Core/InteropServices/SafeMemoryHandle.cs b/Core/InteropServices/SafeMemoryHandle.cs index 79e035f8..8498dbfd 100644 --- a/Core/InteropServices/SafeMemoryHandle.cs +++ b/Core/InteropServices/SafeMemoryHandle.cs @@ -160,6 +160,17 @@ namespace Vanara.InteropServices handle.FillMemory(value, length); } + /// Releases the owned handle without releasing the allocated memory and returns a pointer to the current memory. + /// A pointer to the currently allocated memory. The caller now has the responsibility to free this memory. + public virtual IntPtr TakeOwnership() + { + var h = handle; + SetHandleAsInvalid(); + handle = IntPtr.Zero; + Size = 0; + return h; + } + /// Zero out all allocated memory. public virtual void Zero() => Fill(0, Size);