all groups > dotnet clr > october 2006 >
You're in the

dotnet clr

group:

Is there a way to find what objects are referencing another object


Is there a way to find what objects are referencing another object Scythen
10/25/2006 8:49:02 AM
dotnet clr: I am writing a class library to be used with native C++ programs and I need
to ensure all of the managed objects in the library have been freed. Some of
my managed objects hold unmanaged interfaces provided by the application.
Some of these interfaces are unmanaged D3D interfaces.

The system requires a specific startup and shutdown processes. In particular
I need to ensure all of the managed objects are released in the Shutdown
function.

In Shutdown I delete (or set to null) all references to managed objects and
then force a garbage collection (and wait for it to finish). Unfortunately
some managed objects have survived and live on! What's worse is that some of
them are holding D3D resources that HAVE to be released before D3D is
shutdown.

How can I track down what is referencing these objects?
Re: Is there a way to find what objects are referencing another object Ben Voigt
10/25/2006 1:08:50 PM

[quoted text, click to view]

You need to implement IDisposable to deal with unmanaged resources and not
rely on the garbage collector.

[quoted text, click to view]

Re: Is there a way to find what objects are referencing another ob Scythen
10/30/2006 1:46:01 PM
I do implement IDisposable.
The problem is not that unmanaged resources are not being cleaned up
correctly when they are garbage collected. The problem is that there are
managed objects that are still referenced by something after my shutdown
procedure. So, they are never garbage collected and Dispose is never called.

I’m sure I have left a reference to the objects somewhere that is causing
the problem but finding out where that reference is seems nearly impossible.

I did find one of the references; I had a static class member referencing
something that it shouldn’t have been. What I really need is a way to find
these references so I can remove them.

[quoted text, click to view]
Re: Is there a way to find what objects are referencing another ob Ben Voigt
10/30/2006 4:53:51 PM

[quoted text, click to view]

The garbage collector calls the finalizer, not Dispose. You should call
Dispose yourself when you are finished using the object.

Re: Is there a way to find what objects are referencing another ob Loy
10/31/2006 1:37:49 AM
Hi Scythen

You should use sos.dll debugger extension
Break after you request shutdown
Find the "leaking" object by !sos.dumpheap -type <your type here>
-cache
find one of the references to the object by: !sos.gcroot <your object
address>

Hope it helps

Loy

[quoted text, click to view]
AddThis Social Bookmark Button