6

In one of my applications I observe an increasing number of handles. The number is incremented roughly every second without using the application, so there must be a handle leak in some part of the background processing code.

How can I trace such leaks? Are there any tools to help with this? What are the patterns to look for when tracing handle leaks? What are the most common mistakes that cause a handle leak?

A bunch of questions, but I hope they are related enough to put them in one question. Thanks for any input in advance!

jpfollenius
  • 15,826
  • 9
  • 83
  • 148

2 Answers2

6

I suggest using Process Explorer. Run it as Administrator to make sure you see all the relevant data. There you can observe what kind of handles your process has and which are excessively being created and not free'd. This should help you to narrow down the search (in my case, i had sockets leaking all over the place due to some false assumptions about the behaviour of winsocks.). Perhaps then you can isolate the relevant code and post it here for us to check if there is anything wrong. Good luck!

PeterK
  • 6,166
  • 5
  • 45
  • 80
  • +1 Thanks! I know ProcessExplorer but haven't used it for this purpose. The handles that are leaking are section handles. See my follow-up question: http://stackoverflow.com/questions/7846526/what-can-cause-section-handle-leaks – jpfollenius Oct 21 '11 at 07:55
5

Some profilers (i.e. AQTime) can profile Windows resource applications. If you're using XE, you have a basic version of AQTime available.

Handle leaks are common if an execution path doesn't close them properly - but a in Windows a handle is a very common item used in very many different situations, which kind of handles is your application leaking?

  • +1 Thanks! I have to try AQTime. Turns out that my app is leaking section handles. Please have a look at the follow-up question: http://stackoverflow.com/questions/7846526/what-can-cause-section-handle-leaks – jpfollenius Oct 21 '11 at 07:55
  • 1
    A profiler like AQTime is able to tell you where a leaked handle has been created. Using Process Explorer this is a little more difficult, because it can show you call stacks, but it doesn't record informations the way AQTime does - although AQTime is "heavier" because of that. You could also try Process Monitor which actually records many of those calls. Anyway, if you have a profiler like AQTime available IMHO that is the best tool to accomplish something like this. PE/PM are very valuable tools when you have to inspect a system where you can install AQTime or the like. –  Oct 21 '11 at 13:05