Esempio n. 1
0
        /// <summary>
        /// Frees a block of memory previously allocated with Allocate().
        /// </summary>
        /// <param name="block">The block to free.</param>
        /// <exception cref="InvalidOperationException">There was an error freeing the block.</exception>
        public static void Free(IntPtr block)
        {
            if (_hHeap != IntPtr.Zero)
            {
                long bytes = (long)SafeNativeMethods.HeapSize(_hHeap, 0, block);

                bool result = SafeNativeMethods.HeapFree(_hHeap, 0, block);

                if (!result)
                {
                    int error = Marshal.GetLastWin32Error();
                    throw new InvalidOperationException("HeapFree returned an error: " + error.ToString());
                }

                if (bytes > 0)
                {
                    GC.RemoveMemoryPressure(bytes);
                }
            }
        }