Ryan O'Neill
Ryan O'Neill

Reputation: 5687

How do I find where my .Net app is leaking Windows handles?

I've got a WinForms .Net app that runs over a lot of XPS documents (thousands) and during this run the number of handles (according sysinternals process monitor and task manager) increases by between 3-10 for each document. I heavily suspect that these are handles that are opened and not closed by the MS .Net XPS framework libraries but I can't track it down.

Despite stepping through the code the handle count increases at different points during the same run. For example, one pass through the loop and the handle count will jump by 1 on one line and next time it might not, or it might jump by 2. So I suspect the XPS com components have a handle leak.

Memory use is fine though, but if I keep exhausting handles like this then I'll crash the app and maybe the desktop.

So far I have tried sysinternals process explorer to look at the handles but they are all marked as with no more detail. Also used handle.exe from sysinternals and this does not show any significant different between a before, during and after snapshot.

Any clues on how to track down where the handles are going? I think I'm going to have to simplify to a single threaded console app to test.

Regards

Ryan

Upvotes: 4

Views: 2346

Answers (4)

Jon Cage
Jon Cage

Reputation: 37490

I've had good experiences diagnosing exactly this kind of fault using YourKit .net profiler. Highly recommended - it's relatively cheap and it even copes with C++-CLI reasonably well (I've tried others such as dottrace from jetbrains which either refused or just crashed outright).

Upvotes: 0

user1228
user1228

Reputation:

So, did you forget to remove the XPS Documents you've created form the PackageStore? And what about any MemoryStreams used to back your Packages?

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500873

Possibly stupid, but have you checked that you're disposing everything that's disposable?

Upvotes: 1

Paige Watson
Paige Watson

Reputation: 1214

Although it's a bit expensive for singular use, we use ANTS Profiler and it is quite helpful in finding issues like this. You could download and try the trial version.

There are probably other good profilers out there as well, but this is the one that I am familiar with.

Upvotes: 1

Related Questions