开发者

What exactly is the contract of GCHandle.Alloc(Object)?

I'd like to have a rigorous understanding of the contract of GCHandle.Alloc(Object).

I understand from t开发者_Python百科he 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


Correct. The GCHandle variable has no effect on the lifetime of the object that you pass to GCHandle.Alloc(). I verified this with the following two classes:

class Test
{
   public Test ()
   {
      System.Runtime.InteropServices.GCHandle.Alloc(this);
   }
   ~Test ()
   {
   }
}
class Server : MarshalByRefObject
{
   public Server ()
   {
      new Test();
   }
   public void Collect ()
   {
      GC.Collect();
      GC.WaitForPendingFinalizers();
   }
}

And the following test code:

AppDomain ad = AppDomain.CreateDomain("test");
Server svr = (Server)ad.CreateInstanceAndUnwrap(
   System.Reflection.Assembly.GetExecutingAssembly().FullName, 
   typeof(Server).FullName);
for (Int32 i = 0; i < 100; i++)
   svr.Collect();

Using this example, if you set a breakpoint in the Test class's finalizer, you will note that it is not called during any of the GC.Collect/WaitForPendingFinalizers calls. The breakpoint is hit, though, when the appdomain is unloaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜