/// <summary> /// Frees an unmanaged array and performs cleanup for each value. Optionally can free an array of pointers. This can be used on any type that can be /// marshaled into unmanaged memory. /// </summary> /// <typeparam name="T">Struct type</typeparam> /// <param name="nativeArray">Pointer to unmanaged memory</param> /// <param name="length">Number of elements to free</param> /// <param name="action">Delegate that performs the necessary cleanup</param> /// <param name="arrayOfPointers">True if the pointer is an array of pointers, false otherwise.</param> public static void FreeNativeArray <T>(IntPtr nativeArray, int length, FreeNativeDelegate action, bool arrayOfPointers) where T : struct { if (nativeArray == IntPtr.Zero || length == 0 || action == null) { return; } //If the pointer is a void** we need tp step by the pointer eize, otherwise its just a void* and step by the type size int stride = (arrayOfPointers) ? IntPtr.Size : MarshalSizeOf <T>(); for (int i = 0; i < length; i++) { IntPtr currPos = AddIntPtr(nativeArray, stride * i); //If pointer is a void**, read the current position to get the proper pointer if (arrayOfPointers) { currPos = Read <IntPtr>(currPos); } //Invoke cleanup action(currPos, arrayOfPointers); } FreeMemory(nativeArray); }
static void OverrideFree(GLib.GType gtype, FreeNativeDelegate callback) { unsafe { IntPtr *raw_ptr = (IntPtr *)(((long)gtype.GetClassPtr()) + (long)class_abi.GetFieldOffset("free")); *raw_ptr = Marshal.GetFunctionPointerForDelegate((Delegate)callback); } }
private void InternalFree(Gst.Memory memory) { FreeNativeDelegate unmanaged = null; unsafe { IntPtr *raw_ptr = (IntPtr *)(((long)this.LookupGType().GetThresholdType().GetClassPtr()) + (long)class_abi.GetFieldOffset("free")); unmanaged = (FreeNativeDelegate)Marshal.GetDelegateForFunctionPointer(*raw_ptr, typeof(FreeNativeDelegate)); } if (unmanaged == null) { return; } memory.Owned = false; unmanaged(this.Handle, memory == null ? IntPtr.Zero : memory.Handle); }
/// <summary> /// Frees an unmanaged array and performs cleanup for each value. This can be used on any type that can be /// marshaled into unmanaged memory. /// </summary> /// <typeparam name="T">Struct type</typeparam> /// <param name="nativeArray">Pointer to unmanaged memory</param> /// <param name="length">Number of elements to free</param> /// <param name="action">Delegate that performs the necessary cleanup</param> public static void FreeNativeArray <T>(IntPtr nativeArray, int length, FreeNativeDelegate action) where T : struct { FreeNativeArray <T>(nativeArray, length, action, false); }