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 to an object. it's mainly used for holding onto a managed object that gets passed to the unmanaged world to prevent the GC from collecting the object. you can also create a pinned gchandle to a managed object and retrieve the object's address in memory.
How are gchandles dangerous?
When you create a new gchandle, a new entry in the appdomain's handle table is created. this entry is kept until the handle is freed (via gchandle. free () or the appdomain is unloaded.
Things get tricky if you were to make a copy of a gchandle:
Object OBJ = new object ();
Gchandle gch = gchandle. alloc (OBJ, gchandletype. Normal );
Gchandle gch2 = gch;
Since gchandle is value type, gch2 has its own copy of the handle. you now have two handles that point to the same entry in the handle table. unfortunately, since gch2 is a copy of-not a reference to-gch, anything that happens to gch doesn't happen to gch2. for example, calling gch. free () will delete the entry from the handle table, but not update gch2, so gch2.isallocated will return true, but gch2.target will be null. the same problem arises with casting to and from intptrs, and when gchandles get boxed. unlike double-freeing a single gchandle, freeing the copy will not throw an invalidoperationexception. you have to be very careful not to double-free your handles since this can just upt the handle table.
Why don't gchandles implement idisposable?
One of the main purposes of idisposable to avoid the use of finalizers. this is because finalizers are not run deterministically, and result in promoting a finalizable object a generation, implements tively keeping in memory longer. since gchandle is a value type, it has no finalizer, and is not collected by the GC, so these problems are eliminated. another other main use of idisposable is to clean up unmanaged resources as soon as you are done with them. with a gchandle, the resource is the handle which is cleaned up by calling gchandle. free (). if free isn't called, the handle gets cleaned up when the appdomain is unloaded.
One of the side effects of having a struct implement idisposable, is that users may be tempted to cast their gchandles as idisposable, which boxes the gchandle into an idisposable object on the heap, and the two gchandles get out-of-sync. the same problem arises with the putting a disposable struct into a using block:
Struct test: idisposable
{
Public bool disposed; // initialized to false
Public void dispose ()
{
Disposed = true;
}
}
Public void Foo ()
{
Test T = new test ();
Using (t)
{
// Do stuff
}
If (! T. disposed)
{
T. Dispose ();
}
}
T. disposed will return false, since it was a copy of T whose dispose method was called. if T were a gchandle, then the handle wocould be removed from the appdomain's handle table, and calling free after the using wocould result in a double free, even though isallocated wocould return true!
Remember, gchandles are advanced structures, and one shoshould be very careful to ensure they are cleaned up properly. unfortunately, idisposable makes it easy to get this wrong, so the Bcl designers erred on the side of caution, and gave gchandle a free () method to use.
The truth about gchandles