public void MemoryLeakWhenNotRemovingEventHandler() { DummyEventProvider eventProvider = new DummyEventProvider(); DummyEventHandler eventHandler = new DummyEventHandler(); // Register for event. eventProvider.TestEvent += eventHandler.OnTestEvent; // Don't unregister from event. // Release object. eventHandler = null; // Event handler is not garbage collected. GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(1, DummyEventHandler.InstanceCount); // Release provider. eventProvider = null; // Event handler is garbage collected. GC.Collect(); GC.WaitForPendingFinalizers(); Assert.AreEqual(0, DummyEventHandler.InstanceCount); }