I have a kind of interesting scenario which exceeds my current knowledge. I would expect the following test to succeed, however, it fails unless I force a manual GC.Collect
.
public class Foo : IDisposable {
public void Dispose() {
Debug.WriteLine("Disposed.");
}
}
[Test]
public void CallScopeTest2()
{
var list = new List<WeakReference>();
for (var i = 0; i != 5; ++i)
{
list.Add(RunInner());
// give time to GC
Thread.Sleep(4000);
}
//GC.Collect(); // <--- if I uncomment this line, it will collect my objects and test passes
// give yet a little more time to GC
Thread.Sleep(5000);
var c = list.Count(e => e.IsAlive);
// here c == 5, unless I use the manual collect above
c.ShouldEqual(0);
}
private static WeakReference RunInner()
{
WeakReference result;
using (var foo = new Foo())
{
result = new WeakReference(foo);
}
return result;
}