ExternallyControlledLifetimeManager and Container Strong Reference

Apr 12, 2010 at 8:56 PM
Edited Apr 12, 2010 at 9: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;
  public void TestExternallControlledRegisterInstanceWeakReference()
   o = new TestClass();
   unityContainer.RegisterInstance<ITestInterface>(o, new ExternallyControlledLifetimeManager());
   ITestInterface value = unityContainer.Resolve<ITestInterface>();
   WeakReference wr = new WeakReference(value);
   Assert.IsTrue(wr.IsAlive, "WeakReference not alive immediately after resolution");
   o = null;
   // attempt full GC with one weak reference and one strong reference
   // 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 = null;
   // attempt full GC with one weak reference and no strong reference
   // instance should be collected now

 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 13, 2010 at 12:09 AM

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 2:53 AM
Of course, you're correct. ...been staring a code too long today. Thanks -- Peter