title: What exactly is the contract of GCHandle.Alloc(Object)?
body: <p>I'd like to have a rigorous understanding of the contract of GCHandle.Alloc(Object).</p>
  
  <p>I understand from the documentation that if I call:</p>
  
  <pre><code>GCHandle gc = GCHandle.Alloc(foo);
  </code></pre>
  
  <p>foo will be guaranteed not to be garbage collected until I call:</p>
  
  <pre><code>gc.Free();
  </code></pre>
  
  <p>I also understand that foo will get collected if the AppDomain is unloaded.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>E.g.
  An object calls Alloc on itself and does not retain the GCHandle instance, it will live until the AppDomain is unloaded.</p>
  
  <p>Is this correct?</p>
  
  <p>Some references:</p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/a95009h1.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/a95009h1.aspx</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.free.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.free.aspx</a></p>
  
  <p><a href="http://blogs.msdn.com/b/clyon/archive/2005/03/18/398795.aspx" rel="nofollow">http://blogs.msdn.com/b/clyon/archive/2005/03/18/398795.aspx</a></p>