8

Is there some way to detect file handle leaks at program termination?

In particular I would like to make sure that all of my handles that get created are being freed in code.

For example, I may have a CreateFile() somewhere, and at program termination I want to detect and ensure that all of them are closed.

10 Answers 10

7

If you can (i.e. if it's not a huge legacy code-base you are bugfixing) you should consider using the RAII idiom to wrap around your file handles. By "taking" the file handle in the constructor and releasing it in the destructor you can be sure that by the time your RAII goes out of scope your file handle is nicely cleaned up too.

It's the same principle as smart pointers, and it's a very useful concept to have in your toolbox for avoiding issues like this in C++.

6

I have used !htrace command of windbg.

!htrace -enable
!htrace -snapshot
!htrace -diff

Allow you to compare the handle situation of two execution point and help you the locate the point where the leaked handle have been allocated.

It worked well for me.

1
  • will this give call stack of allocated handles between snapshots?
    – paulm
    May 29, 2014 at 22:40
4

You can also use MS's Application Verifier.

2
  • A brief read of the documentation doesn't reveal anything about handle leak detection. What am I missing? Jan 14, 2009 at 11:51
  • the trick is that the code has to be in a dll, when it unloads the leak detection will pick up leaked handles and leaked heap allocs
    – paulm
    May 29, 2014 at 22:41
4

If you can't afford BoundsChecker or similar...

One trick I've used is to replace CreateFile etc. with my own wrappers. In addition to returning the handle value, they make a record of the __FILE__ and __LINE__ to go with each handle. You'll need to wrap CloseHandle as well, to make sure that properly-closed handles don't raise false positives.

It's as simple as:

// StdAfx.h
#include <windows.h>
#undef CreateFile
#if defined(UNICODE)
 #define CreateFile DbgCreateFileW
#else
 #define CreateFile DbgCreateFileA
#endif

// etc.

You then define DbgCreateFileW and DbgCreateFileA somewhere in your code.

This assumes that you've got control over the relevant pieces of code. If not, you can do something similar by using (e.g.) Microsoft Detours (you'll need a license to include it in a released product, but I believe that it's free to use for debugging/testing/etc.)

Long term, however, you should look at converting your code to use a "smart" handle type that automatically calls CloseHandle when it goes out of scope.

1

BoundsChecker or other similar programs will do that. I also thought that running under the debugger in VC6 and above would report the list of leaks. Perhaps this is because I always had plugin tools to do this sort of thing.

1

Use smart pointers for file handles and similar resources.

1

Another tip would be the SysInternals FileMon (Link).

It shows you the file system acitivity, so it should support you in finding open handles.

1

I suggest this tool Memory Validator

it is very nice.

0

As MadKeithV stated, you can use RAII to great effect.

Also a simple way (and free) is to use Visual Leak Detector. It's not perfect but a good way of making sure you have free'd/delete[]d/close'd, etc.

0

windbg or ntsd have a !handle extension that tells you what handles an app has open. I suppose you could put a breakpoint at the end of your program and dump the handles there.

There may also be something you can do in powerdbg (http://www.codeplex.com/powerdbg) to automate this process.

Of course, this assumes your time is cheaper than buying a solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.