Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,38 @@ public void DoNotLeakWeakReferences ()

Assert.IsTrue (surfaced.All (s => s.Target != null), "#1");

// `Runtime.GetSurfacedObjects()` is process-global: NUnit/MTP
// infrastructure (per-test TestExecutionContext, listeners, Console
// capture, etc.) creates and releases transient Java.Lang.Object
// peers around every test, so the count drifts by a few entries
// between the snapshots below. Allow a small tolerance instead of
// requiring an exact count -- a real leak would dwarf this.
const int tolerance = 10;

WeakReference r = null;
Exception threadException = null;
var t = new Thread (() => {
var c = new MyCb ();
Assert.AreEqual (startCount + 1, Runtime.GetSurfacedObjects ().Count, "#2");
r = new WeakReference (c);
try {
var c = new MyCb ();
Assert.That (Runtime.GetSurfacedObjects ().Count,
Is.EqualTo (startCount + 1).Within (tolerance), "#2");
r = new WeakReference (c);
} catch (Exception e) {
threadException = e;
}
});
t.Start ();
t.Join ();
if (threadException != null)
throw new Exception ("Worker thread failed", threadException);
Comment thread
jonathanpeppers marked this conversation as resolved.

GC.Collect ();
GC.WaitForPendingFinalizers ();
GC.Collect ();
GC.WaitForPendingFinalizers ();

surfaced = Runtime.GetSurfacedObjects ();
Assert.AreEqual (startCount, surfaced.Count, "#3");
Assert.That (surfaced.Count, Is.EqualTo (startCount).Within (tolerance), "#3");
Assert.IsTrue (surfaced.All (s => s.Target != null), "#4");
}
}
Expand Down
Loading