The Truth About GCHandles

I’ve heard several people asking why GCHandle doesn’t implement IDisposable, considering it wraps an unmanaged resource (a handle) and needs to be explicitly freed (using GCHandle.Free()). Before I explain the reason, I want to give a little background on GCHandles and their dangers. What’s a GCHandle? A GCHandle is a struct that contains a handle…

6

GCHandles, Boxing and Heap Corruption

A GCHandle is a struct used to hold onto a managed object to be used by unmanaged code.  With a GCHandle you can (among other things): Prevent an object from being garbage collected if unmanaged code has the only live reference to it Pin an object in memory, so it won’t be relocated in memory…

10