public void GCCallbackTest() { bool collectedTextView = false; TextView textView = new TextViewWithGCCallback(delegate { collectedTextView = true; }); textView = null; GarbageCollect(); Assert.IsTrue(collectedTextView); }
// using a method to ensure the local variables can be garbage collected after the method returns void DocumentDoesNotHoldReferenceToLineMargin_CreateMargin(TextDocument textDocument, Action finalizeAction) { TextView textView = new TextViewWithGCCallback(finalizeAction) { Document = textDocument }; LineNumberMargin margin = new LineNumberMargin() { TextView = textView }; }
public void DocumentDoesNotHoldReferenceToTextView() { bool collectedTextView = false; TextDocument textDocument = new TextDocument(); Assert.AreEqual(0, textDocument.LineTrackers.Count); TextView textView = new TextViewWithGCCallback(delegate { collectedTextView = true; }); textView.Document = textDocument; Assert.AreEqual(1, textDocument.LineTrackers.Count); textView = null; GarbageCollect(); Assert.IsTrue(collectedTextView); // document cannot immediately clear the line tracker Assert.AreEqual(1, textDocument.LineTrackers.Count); // but it should clear it on the next change textDocument.Insert(0, "a"); Assert.AreEqual(0, textDocument.LineTrackers.Count); }