views:

1429

answers:

10

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.

+1  A: 

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.

Tim
+3  A: 

You can also use MS's Application Verifier.

John Dibling
A brief read of the documentation doesn't reveal anything about handle leak detection. What am I missing?
Roger Lipscombe
+1  A: 

Use smart pointers for file handles and similar resources.

Marcin
+4  A: 

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++.

MadKeithV
+1  A: 

Another tip would be the SysInternals FileMon (Link).

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

bernhardrusch
+3  A: 

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.

Roger Lipscombe
A: 

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.

graham.reeds
+1  A: 

I suggest this tool Memory Validator

it is very nice.

Ahmed Said
A: 

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 :)

+1  A: 

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.

Vivian De Smedt