[Mono-dev] WeakReference problem
Tobias Käs
tobias.kaes at gmx.de
Thu Jan 30 17:56:33 UTC 2014
While trying to write a minimal test case I realized that mono actually
never (?) returns false from WeakReference.TryGetTarget when the object
has been collected, so this is pretty easy to reproduce and totally
deterministic.
using System;
using System.Threading;
static class Program {
static WeakReference<object> wr;
static void Main(string[] args) {
Allocate();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Check();
}
static void Allocate() {
wr = new WeakReference<object>(new object());
}
static void Check() {
object obj;
if(wr.TryGetTarget(out obj) && obj == null) {
throw new ApplicationException("assertion
failed: object has been collected but TryGetTarget returned true");
}
}
}
Am 29.01.2014 23:41, schrieb Rodrigo Kumpera:
> Please provide a test case that shows this behavior
>
>
> On Wed, Jan 29, 2014 at 5:00 PM, Tobias Käs <tobias.kaes at gmx.de
> <mailto:tobias.kaes at gmx.de>> wrote:
>
> Hi, the generic WeakReference<T> for .NET 4.5 sometimes returns
> true from TryGetTarget even though the target has been collected
> (the out-value is correctly set to null). All my WeakReferences
> have been initialized with non-null values.
>
> Looking at the source at github I can imagine that there is a race
> between the handle.IsAllocated check and the handle.Target
> retrieval, but I'm no expert. Should I file a bug on that one? I'm
> not sure what the expected behavior is since MSDN documentation of
> that method isn't so great, but if the caller has to check the
> out-value against null anyways, then the return value becomes
> kinda pointless in my opinion.
>
> Regards,
> Tobias
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> <mailto:Mono-devel-list at lists.ximian.com>
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20140130/35362900/attachment.html>
More information about the Mono-devel-list
mailing list