Skip to main content

Practice · 1 of 2

Diagnose the event leak

CHALLENGE
Difficulty: advanced+25 XP

Event subscriptions root their subscribers: a long-lived publisher keeping short-lived subscribers alive is .NET's classic leak. Implement static int AliveAfterUnsubscribe() that: creates a Publisher, creates 100 Subscriber objects each subscribing to Publisher.Tick, returns 100; then implement static void UnsubscribeAll(Publisher p, List<Subscriber> subs) calling Dispose on each (subscribers implement IDisposable and unsubscribe in Dispose). The test verifies that after Dispose + GC.Collect x2, a WeakReference to a subscriber is dead — proving unsubscribe breaks the root.

Back to lesson: Practice: GC drills