ReleaseGCObject(ulong)
Releases a GC handle obtained from UnsafeUtility.PinGCObjectAndGetAddress or UnsafeUtility.PinGCArrayAndGetDataAddress.
Read time 1 minuteLast updated 12 days ago
Definition
- Type: Method
- Namespace: Unity.Collections.LowLevel.Unsafe
- Assembly: UnityEngine.ManagedKernelModule
public static void ReleaseGCObject(ulong gcHandle)
Parameters
The handle associated with the pinned object.
Remarks
The method unpins an object, allowing the garbage collector to manage its memory freely. Use this method to prevent resource leaks. Ensure you release each handle after finishing direct memory operations.
ReleaseGCObjectAdditional Resources: UnsafeUtility.PinGCObjectAndGetAddress, UnsafeUtility.PinGCArrayAndGetDataAddress
Examples
using Unity.Collections.LowLevel.Unsafe;using UnityEngine;public class ReleaseGCObjectExample : MonoBehaviour{ private struct MyStruct { public int value; } void Start() { // Box a struct object boxedStruct = new MyStruct { value = 10 }; // Pin the object and get a GC handle unsafe { void* dataPtr = UnsafeUtility.PinGCObjectAndGetAddress(boxedStruct, out ulong gcHandle); // Access and modify data int objectHeaderSize = UnsafeUtility.SizeOf<long>() * 2; int* valuePtr = (int*)((byte*)dataPtr + objectHeaderSize); *valuePtr = 20; // Release the GC handle UnsafeUtility.ReleaseGCObject(gcHandle); } // Verify modification MyStruct modifiedStruct = (MyStruct)boxedStruct; Debug.Log($"Modified value: {modifiedStruct.value}"); // Outputs: 20 // Confirm handle release Debug.Log("GC handle released."); }}