<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    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.<br>
    <br>
    <br>
    using System;<br>
    using System.Threading;<br>
    <br>
    static class Program {<br>
            static WeakReference<object> wr;<br>
    <br>
            static void Main(string[] args) {<br>
                    Allocate();<br>
                    GC.Collect();<br>
                    GC.WaitForPendingFinalizers();<br>
                    GC.Collect();<br>
                    Check();<br>
            }<br>
    <br>
            static void Allocate() {<br>
                    wr = new WeakReference<object>(new object());<br>
            }<br>
    <br>
            static void Check() {<br>
                    object obj;<br>
                    if(wr.TryGetTarget(out obj) && obj == null)
    {<br>
                            throw new ApplicationException("assertion
    failed: object has been collected but TryGetTarget returned true");<br>
                    }<br>
            }<br>
    }<br>
    <br>
    <br>
    <div class="moz-cite-prefix">Am 29.01.2014 23:41, schrieb Rodrigo
      Kumpera:<br>
    </div>
    <blockquote
cite="mid:CACmR+BAD1NA1dWjb-JWmLVZazaODYCZSkg5fC=od9tg7HrfPVg@mail.gmail.com"
      type="cite">
      <div dir="ltr">Please provide a test case that shows this behavior</div>
      <div class="gmail_extra"><br>
        <br>
        <div class="gmail_quote">On Wed, Jan 29, 2014 at 5:00 PM, Tobias
          Käs <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:tobias.kaes@gmx.de" target="_blank">tobias.kaes@gmx.de</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">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.<br>
            <br>
            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.<br>
            <br>
            Regards,<br>
            Tobias<br>
            _______________________________________________<br>
            Mono-devel-list mailing list<br>
            <a moz-do-not-send="true"
              href="mailto:Mono-devel-list@lists.ximian.com"
              target="_blank">Mono-devel-list@lists.ximian.com</a><br>
            <a moz-do-not-send="true"
              href="http://lists.ximian.com/mailman/listinfo/mono-devel-list"
              target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><br>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>