Reputation: 18827
So Ive got my WebClient wrapped in a using statement. But I suddenly wondered do I need to unsubscribe from events if my object implements IDisposable and is wrapped in a using statment?
Below is my example code of how I am currently using my WebClient
. Will this code currently have memory leaks as it is not unsubscribing from events or will the dispose method deal with that?
using (WebClient wc = new WebClient())
{
wc.DownloadProgressChanged += ((sender, args) =>
{
//monitor something
});
wc.DownloadFileCompleted += ((sender, args) =>
{
//do something
});
wc.DownloadFileAsync(new Uri(string.Format("{0}/{1}", Settings1.Default.WebPhotosLocation, Path.GetFileName(f.FullName))), filePath);
}
Upvotes: 2
Views: 876
Reputation: 6793
No, there is no need. Once the WebClient is collected by the GC all of the event handlers will be cleaned up for you. Before GC occurs they'll do no harm as it won't be possible for the events to fire so the handlers will never be called.
Upvotes: 2