﻿# Delegate.BeginInvoke() and memory leaks...

<datetime class="hidden">2004-02-20T00:00</datetime>
<!-- category -- mostlylucidcouk, Imported, C#, .NET -->

I'm getting increasingly involved in the world of Asynchrony and have been doing a bit of digging about into some of the pitfalls of multi-threading. 
I came across an interesting one today involving the use of BeginInvoke without a corresponding EndInvoke when using delegates...many books (including the one I'm using most right now, the excellent [Programming .NET Components by Juval Lowy](http://www.amazon.co.uk/exec/obidos/ASIN/0596003471/mostlylucid-21)) mention that a good way to fire events asynchronously (i.e., not blocking) is to simple call BeginInvoke on the delegate - cool seems easy enough! 
Well, reading around a bit, I discovered [this by Mike Woodring](http://www.bearcanyon.com/dotnet/#FireAndForget) which contains this statement: *"Starting with the 1.1 release of the .NET Framework, the SDK docs now carry a caution that mandates calling EndInvoke on delegates you've called BeginInvoke on in order to avoid potential leaks. This means you cannot simply "fire-and-forget" a call to BeginInvoke without the risk of running the risk of causing problems. "* Now, I can't find the bit in the docs which says that, but [another article](http://www.ondotnet.com/pub/a/dotnet/2003/02/24/asyncdelegates.html?page=2) at [O'Reilly](http://www.ondotnet.com/dotnet/) mentions the same thing... Well, based on this evidence, it would seem to be a good idea to avoid this potential leak, luckily [Mike's stuff](http://www.bearcanyon.com/dotnet/#FireAndForget) has a handily little helper class which supports the ['Fire and Forget idiom'](http://www.bearcanyon.com/dotnet/#FireAndForget) - dead easy, now I can have lovely fire and forget calls without worrying about unintended leaks :-)

UPDATE: Found the doc reference, if you have MSDN it's [here](https://web.archive.org/web/ms-help://ms.netframeworksdkv1.1/cpguidenf/html/cpovrasynchronousprogrammingoverview.htm), if not the on-line version is [here](http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp)