public void IndirectDependentObjectCanBeGarbageCollected() { GC.Collect(); SourceData independent = new SourceData(); DirectDependent intermediate = new DirectDependent(independent); IndirectDependent indirectDependent = new IndirectDependent(intermediate); independent.SourceProperty = 42; Assert.AreEqual(42, indirectDependent.DependentProperty); WeakReference weakIndirectDependent = new WeakReference(indirectDependent); GC.Collect(); Assert.IsTrue(weakIndirectDependent.IsAlive, "Since we hold a strong reference to the dependent, the object should still be alive."); // This assertion here to make sure the dependent is not optimized away. Assert.AreEqual(42, indirectDependent.DependentProperty); indirectDependent = null; GC.Collect(); Assert.IsFalse(weakIndirectDependent.IsAlive, "Since we released the strong reference to the dependent, the object should not be alive."); // Make sure we can still modify the independent, and that the intermediate still depends upon it. independent.SourceProperty = 32; Assert.AreEqual(32, independent.SourceProperty); Assert.AreEqual(32, intermediate.DependentProperty); }
public void Initialize() { _source = new SourceData(); _intermediateDependent = new DirectDependent(_source); _dependent = new IndirectDependent(_intermediateDependent); }
public void Initialize() { _source = new SourceData(); _intermediateDependent = new DirectDependent(_source); _dependent = new IndirectDependent(_intermediateDependent); }
public void IndirectDependentObjectCanBeGarbageCollected() { GC.Collect(); SourceData independent = new SourceData(); DirectDependent intermediate = new DirectDependent(independent); IndirectDependent indirectDependent = new IndirectDependent(intermediate); independent.SourceProperty = 42; Assert.AreEqual(42, indirectDependent.DependentProperty); WeakReference weakIndirectDependent = new WeakReference(indirectDependent); GC.Collect(); Assert.IsTrue(weakIndirectDependent.IsAlive, "Since we hold a strong reference to the dependent, the object should still be alive."); // This assertion here to make sure the dependent is not optimized away. Assert.AreEqual(42, indirectDependent.DependentProperty); indirectDependent = null; GC.Collect(); Assert.IsFalse(weakIndirectDependent.IsAlive, "Since we released the strong reference to the dependent, the object should not be alive."); // Make sure we can still modify the independent, and that the intermediate still depends upon it. independent.SourceProperty = 32; Assert.AreEqual(32, independent.SourceProperty); Assert.AreEqual(32, intermediate.DependentProperty); }