C# MSMQ BeginPeek/PeekCompleted memory leak - Need some help
Posted on 2005-04-13
I've got an server app in C# that is basically sitting there waiting for messages to arrive in MSMQ from somewhere else. I use a BeginPeek( timeout, IASyncResult) call to start the whole process. When the peekCompleted event fires, I disconnect the event handler and then I check to see why it fired. If it's because of a message I deal with the messages. If it is because the timeout expired, I reattach the event handler and call BeginPeek() again.
The problem is that my application is sitting idle, with no messages in the queue and each iteration of BeginPeek/PeekCompleted is leaking object handles. I discovered this by looking at the Task Manager at first. Then by using the .Net Memory Profiler by Sci-Tech. I was able to track down where it was occuring.
Each iteration is leaking 1 Message object and one ManualResetEvent object.
But there can't be any message object because there aren't any messages in the queue! I'm not storing a reference to what EndPeek() returns because I don't care what it is if it's a message my message handling code will take care of it. If its not a message all I need to do is move on and start the 'loop' over again.
Anyone have any idea what's going on or better yet how to fix it? I'm leaking 2 handles every 3 seconds or so and obviously that's a problem. I can post code if need be. It's not really all that complicated what I'm doing which is why it's so confusing and aggravating that it's leaking memory.