Welcome to MSDN Blogs Sign in | Join | Help

WeakReferences And Tracking Resurrection

The WeakReference class has two public constructors. 

 

public WeakReference(Object target)

public WeakReference(Object target, bool trackResurrection)

 

The first parameter is pretty obvious.  It’s the object you want the WeakReference to reference, without keeping the object alive.  If a WeakReference is the only thing referencing an object, then the GC is free to collect the object.  After the GC collects the object, WeakReference.IsAlive will return false, and WeakReference.Target will return null.  The WeakReference loses its handle to the object as soon as the GC collects it but before the finalizer (if any) gets run.  This is called a short WeakReference.

 

If the caller passes true as the trackResurrection parameter, then the WeakReference tracks the object’s life until finalization is complete.  This is called a long WeakReference.  If the object is resurrected, a long WeakReference will continue to reference it, where a short WeakReference will report it as dead.

 

The same distinction also applies to GCHandleType.Weak.

Published Monday, May 01, 2006 5:23 PM by clyon
Filed under:

Comments

# Is WeakReference a weak feature?

Thursday, October 11, 2007 9:28 PM by Rogerio's

Weak references is a feature where one object can point to another without holding it. In other words,

# MSDN Blog Postings » Is WeakReference a weak feature?

Thursday, October 11, 2007 11:37 PM by MSDN Blog Postings » Is WeakReference a weak feature?

# Feed Search Engine - All Fresh Articles And News Are Here

Sunday, November 25, 2007 9:10 AM by Feed Search Engine - All Fresh Articles And News Are Here
Anonymous comments are disabled
 
Page view tracker