Debugging file leaks - the easy way

Ever had problems with opening file that surely is present at given path? It most probably is the case of “too many open handles” problem. First of all, make sure it really is the reason, it should be easy enough with our new friend – pseudoregisters, just examine error code. The most common reason for this are file leaks (file is opened, but never closed). Files are “normal” Windows resources so you can use tools like htrace for analysis. However, in case of application like game, which in most cases doesnt open that many files and there are only few points where they can be open (inside your resource manager, most usually), you can use easier method. In many situations you know (roughly) what files should be open at any given point, so if we could get the list of open handles we could also quickly determine which of them shouldnt be there. It’s time to meet Process Explorer. It basically is Task Manager on steroids. It’s written by Mark Russinovich of SysInternals, who apparently traded his soul to the devil for mad hacking skills. If you’re a programmer, there’s a good chance you have it installed already, if not – download it now. All there’s left now is to launch your game, get to the point where an error occurs and examine open file handles (Show Lower Pane in PE).

Process Explorer showing Firefox.exe handles.

In 90% of cases you’ll be able to tell instantly what files shouldnt be open at this point. 6 months ago we had a problem where gzclose (zlib function) in some situations wouldnt close our files. It took me several hours to find this bug. Now it would be 2 minutes. While we’re at it, make sure to check out other SysInternals tools, there’s some crazy stuff there. In my next incarnation, if I cant be Adriana’s Lima’s pillow, I want to be like Mark.

Old comments

Maciej Miasik 2008-02-23 19:54:39

I always thought it should be Adriana’s Victoria’s Secred underwear not pillow. But I’m an old school guy…

Maciej Miasik 2008-02-23 19:55:33

O in case of Victoria’s secret, the right word is Lingerie.

admin 2008-02-24 10:11:02

I’m a minimalist, pillow would do :)

Maciej Miasik 2008-03-02 13:34:40

It would do, but you should try to reach higher:
http://tinyurl.com/34mbzd
At least in your dreams :)

Lola Allen 2010-05-02 00:41:53

Adriana Lima is my favorite Victoria Secret model. She has perfect curves and a very pretty face.’~;