ExternallyControlledLifetimeManager and Container Strong Reference

Apr 12, 2010 at 7:56 PM
Edited Apr 12, 2010 at 8:12 PM

In the documentation for RegisterInstance (Unity Application Block 1.2 Oct 2008 PDF p98) it details that using ExternallyControlledLifetimeManager means the container holds only weak reference to the object. This means that the object should be "collectable" when my code no longer has a strong reference to the instance. I'm finding that this isn't the case. For example, the following unit tests fails at the first Assert.IsFalse: 

  private ITestInterface o;
  [TestMethod]
  public void TestExternallControlledRegisterInstanceWeakReference()
  {
   o = new TestClass();
   unityContainer.RegisterInstance<ITestInterface>(o, new ExternallyControlledLifetimeManager());
   ITestInterface value = unityContainer.Resolve<ITestInterface>();
   WeakReference wr = new WeakReference(value);
   Assert.IsNotNull(wr.Target);
   Assert.IsTrue(wr.IsAlive, "WeakReference not alive immediately after resolution");
   o = null;
   // attempt full GC with one weak reference and one strong reference
   GC.Collect();
   GC.WaitForFullGCComplete();
   // object should be collected due to no remaning strong references
   Assert.IsFalse(wr.IsAlive, "WeakReference should not be alive due to no strong references");
   // remove our strong reference
   value = null;
   // get rid of our container object
   unityContainer.Dispose();
   unityContainer = null;
   // attempt full GC with one weak reference and no strong reference
   GC.Collect();
   GC.WaitForFullGCComplete();
   // instance should be collected now
   Assert.IsFalse(wr.IsAlive);
  }

 Similar tests with no explicit LifetimeManager and an explicit ContainerControlledLifetimeManager work as expected. (i.e. this same test with RegisterType and no explicit LifetimeManager passes).

Apr 12, 2010 at 11:09 PM

Your test is incorrect. At the point of the first assert, you still have a strong reference in the "value" variable. It's alive until you set it to null the next line down.

 

Apr 13, 2010 at 1:53 AM
Of course, you're correct. ...been staring a code too long today. Thanks -- Peter