2006-12-02

WeakReference woes!

Take a look at the following code

if (myWeakReference.IsAlive) (myWeakReference.Target as SomeClass).DoSomething();

Do you see the mistake? The WeakReference may return "true" for IsAlive, but because the garbage collector runs within its own thread the value may actually get collected before the next line (because WeakReference.Target does not prevent the GC from collecting the value). I've been using WeakReferences quite a lot recently so I was happy to see that the following changes fixed the occasional NullReferenceException occurencies I had been seeing which were quite difficult to track down!

The IsAlive property in my opinion is utterly useless (it is the same implementation as Target). I think MS should just remove it, you could say it would break existing code, but I say it would force people to fix it!

2 comments:

The MSDN documentation clearly explains this problem with IsAlive: "using this property is not recommended unless you are testing only for a false return value".

As this suggests, IsAlive is useful for finding dead references, e.g. if you have a dictionary of weak references, you can go through and easily identify the dead references to be removed using the IsAlive property.