I'd like to have a rigorous understanding of the contract of GCHandle.Alloc(Object).
I understand from the documentation that if I call:
GCHandle gc = GCHandle.Alloc(foo);
foo will be guaranteed not to be garbage collected until I call:
gc.Free();
I also understand that foo will get collected if the AppDomain is unloaded.
What I would like to check is if a call to Alloc without a call to Free is effectively the same (in the eyes of the GC) as a root reference.
To be clear if this is true then the scope of the GCHandle variable gc has no impact on the lifetime of foo. If Free is not called foo lives until the AppDomain unloads.
E.g. An object calls Alloc on itself and does not retain the GCHandle instance, it will live until the AppDomain is unloaded.
Is this correct?
Some references:
http://msdn.microsoft.com/en-us/library/a95009h1.aspx
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.free.aspx
http://blogs.msdn.com/b/clyon/archive/2005/03/18/398795.aspx