posts - 916, comments - 758, trackbacks - 11

My Links



Post Categories

Misc. Coding

Delegate.BeginInvoke() and memory leaks...

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) 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 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 at O'Reilly 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 has a handily little helper class which supports the 'Fire and Forget idiom' - 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, if not the on-line version is here

Print | posted on Wednesday, November 19, 2003 6:54 PM | Filed Under [ .NET Multi-Threading ]

Comments have been closed on this topic.

Powered by: