views:

545

answers:

3

Hi all,

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

+1  A: 

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

Jon Skeet
Yes, as I say, the memory use is OK now (had a lot of problems with a leak in a COM XPS call) so I think that dispose is not the problem. I heavily suspect the XPS stuff but I can't prove it yet.
Ryan ONeill
Dispose doesn't really deal with memory - it's more to do with releasing unmanaged resources (handles). I'm not saying it *is* that, but it's worth checking.
Jon Skeet
+1 - use FxCop to help detect places where you may be failing to dispose.
Joe
+1  A: 

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.

Paige Watson
I've used that but I think it will only show .Net handles where these are being leaked at a lower level from the framework I suspect (in this case a lot of it is COM).
Ryan ONeill
As far as I know, .Net profiler cannot detect unmanaged handle leaks. But I manage to narrow down by manually stepping through. Supprisingly, the culprit wasn't COM, but Image.FromStream (.net v1.1)
faulty
A: 

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?

Will
No, this was merely opening and iterating an XPS doc. I did not create any objects. I tracked it down to an XPS operation and figured a workaround (as detailed in another of my questions).
Ryan ONeill