Dispose problem if Owner == Container and ReuseScope == None

Jan 3, 2010 at 7:47 PM
Edited Jan 9, 2010 at 8:15 AM

Consider the following test. It fails.

It means, that in the case Owner == Container and ReuseScope == None the container doesnt guarantee, that all objects will be disposed. This problem is induced by weak references to created objects.

public class Disp : IFoo, IDisposable
    {
      public static int i;
      public void Dispose()
      {
        i++;
      }
    }
    [TestMethod]
    public void ContainerOwnedNonReuseInstacesAreDisposed()
    {
      var container = new Container();
      container.Register<IFoo>(c => new Disp())
        .ReusedWithin(ReuseScope.None)
        .OwnedBy(Owner.Container);

      container.Resolve<IFoo>();

      GC.Collect();

      container.Dispose(); 

      Assert.AreEqual(1, Disp.i);
    }

Coordinator
Oct 2, 2010 at 5:44 AM

if your object holds on to unmanaged resources and really needs disposable, you should implement the finalizable pattern and that would cover this scenario perfectly.

if funq kept those references just to dispose them later on, they would never die, and for a global factory container that would mean infinite memory leak.