Binarysharp.MemoryManagement.Memory.MemoryCore.Free C# (CSharp) Method

Free() public static method

Releases a region of memory within the virtual address space of a specified process.
public static Free ( SafeMemoryHandle processHandle, IntPtr address ) : void
processHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to a process.
address System.IntPtr A pointer to the starting address of the region of memory to be freed.
return void
        public static void Free(SafeMemoryHandle processHandle, IntPtr address)
        {
            // Check if the handles are valid
            HandleManipulator.ValidateAsArgument(processHandle, "processHandle");
            HandleManipulator.ValidateAsArgument(address, "address");

            // Free the memory
            if (!NativeMethods.VirtualFreeEx(processHandle, address, 0, MemoryReleaseFlags.Release))
            {
                // If the memory wasn't correctly freed, throws an exception
                throw new Win32Exception(string.Format("The memory page 0x{0} cannot be freed.", address.ToString("X")));
            }
        }

Usage Example

Example #1
0
 /// <summary>
 /// Releases the memory used by the region.
 /// </summary>
 public void Release()
 {
     // Release the memory
     MemoryCore.Free(MemorySharp.Handle, BaseAddress);
     // Remove the pointer
     BaseAddress = IntPtr.Zero;
 }