I've following C# code in my WPF application and have a question on detaching the event.
public class Publisher
{
public event EventHandler Completed;
public void Process()
{
// do something
if (Completed != null)
{
Completed(this, EventArgs.Empty);
}
}
}
public class Subscriber
{
public void Handler(object sender, EventArgs args) { }
}
Usage:
Publisher pub = new Publisher();
Subscriber sub = new Subscriber();
pub.Completed += sub.Handler;
// this will invoke the event
pub.Process();
My question here is, if I dont unsubscribe the handler method and set objects to null using following lines of code, would it cause any memory leak in the application?
pub.Completed -= sub.Handler
pub=null;sub=null;