Search code examples
c#.netgarbage-collection

Does GCHandle.Free() simply releases the handle?


Just to make sure, given that I have pinned byte array and a GCHandle on this pinned array.

When the GCHandle.Free() method is called, does it simply release the handle and hands over the management of the original byte array back to the GC, therefore if there are active references to that array, it won't be disposed just yet?


Solution

  • You are right. GCHandle is just another handle to the same object, as long as you have at least one, your object won't be disposed.

    However, you can allocate GCHandle with Weak type, which will allow GC to collect your object if your GCHandle is the only one pointing to it (if you have 'normal' reference as well, nothing bad will happen).