views:

44

answers:

2

I have been searching for a tool to debug unreleased COM references, that usually cause e.g. Word/Outlook processes to hang in memory in case the code does not call Marshal.ReleaseCOMObject on all COM instances correctly. (Outlook 2007 partially fixes this for outlook addins, but this is a generic question).

Is there a tool that would display at least a list of COM references (by type) held by managed code? Ideally, it would also display memory profiler-style object trees helping to debug where the reference increment occured.

Debugging at runtime is not that important as being able to attach to a hung process - because the problem typically occurs when the code is done with the COM interface and someone forgot to release something - the application (e.g. winword) hangs in memory even after the calling managed application quits.

If such tool does not exist, what is the (technical?) reason? It would be very useful for debugging a lot of otherwise very hard to find problems when working with COM interop.

A: 

Not quite the answer (or as simple as) you are looking for, but ReleaseCOMObject (as does the native Release which it wraps) returns an Integer which should indicate the number of outstanding references remaining. You could add code to your project to explicitly do an AddRef so you could then Release, and see the new count, and see if it is what you are expecting, etc.

Andy Jacobs
Does not help at all when the cause of the hang is for example using an enumerator, or forgetting that "train" property access requires each instance to be released, e.g. document.Sections[0].Ranges[0].Text.
Marek
I admit it is not as clean or simple as what you are looking for. But if you made a subroutine to AddRef and Release, and report the returned value, and called this before and after all uses of the object (or just the ones you are suspicious of, to reduce the effort), you'd get a better picture of what is happening. E.g., if you did this before and after using the enumerator, you could see if something in that code was leaving a reference around.
Andy Jacobs
A: 

Here is my answer to debugging COM references using debugger

http://stackoverflow.com/questions/2374811/troubleshooting-a-com-application-deadlock/2569801#2569801

Naveen