You're right, mono only returns false if the WR has been finalized. Thanks
for taking the
time to find a way to reproduce the issue. I just fixed it on master.


On Thu, Jan 30, 2014 at 12:56 PM, Tobias Käs <tobias.k...@gmx.de> wrote:

>  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.k...@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@lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>
>
>
>
_______________________________________________
Mono-devel-list mailing list
Mono-devel-list@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

Reply via email to