Beware of local scoping in .NET

Last week Jordan Parker and I were looking at a chunk of code that was evidently leaking memory.  What we discovered wasn’t obvious at first so I thought I’d share it with the world.  The code looked something like this: void BackgroundThreadProc() { while (true) { _waitEvent.WaitOne(); while (!WorkQueueEmpty()) { WorkItem item = DequeueWorkItem(); ProcessWorkItem(item);…

3