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.
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++.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With