Garbage collector and event handlers

Viewed 2692

A quick question. Say that I have a class implemented as in below example.

class Subscriber
{
    private Publisher publisher = new Publisher;


    public Subscriber()
    {
       publisher.SomeEvent += new EventHandler(OnEventFired);
    }

    private void OnEventFired(object sender, EventArgs e)
    {
    }

}

And somewhere in the program I have a method that looks like this:

public void DoSomething()
{
    Subscriber subscriber = new Subscriber();
}

Am I right to expect that this would cause a memory leak since subscriber never unsubscribes from publishers event, thus resulting in them both maintaining strong reference to each other?

2 Answers
Related