How to trace strong references to object instances? RRS feed

  • Question

  • I’m working with a team on a modular application that is keeping alive a lot of managed objects after they were supposed to be finalized.  I’ve found little built into VS2008 to help.  But there is the SOS.dll extension which is somewhat pain staking to use, and other 3rd party tools that make it much easier.  However to use a tool like these is to pro-actively check.  I’d like to have some tracing in our application which will raise flags during development of a module that it isn’t being freed when it should.  Most modules have their own Close method and at this point a simple trace warning could be written if strong references remain.   

    Does anyone have a suggestion of how I could do such tracing?


    Wednesday, June 4, 2008 12:55 AM

All replies

  • That doesn't seem practical.  For one, there is at least one active reference to the object, no way to call Close() otherwise.  How would you know that this reference would not get leaked?  For another, there may be many other references that are themselves no longer referenced and would get garbage collected on the next sweep.  You would have to evaluate the complete allocation graph, much like the garbage collector does.  But without the benefits of generations and concurrent collections and native C++ code walking the heap and thread stacks.  Your program will grind to a halt.
    Hans Passant.
    Wednesday, June 4, 2008 3:50 AM
  • This is a situation where there is a direct parent to child relationship.  I'd like to be able to detect any root paths of child references that do not go through the parent. The parent object would be doing the checking.  This same parent to child relationship persists from a controller to form to user controls.

    I'd certainly expect a significant performance cost however it's something that would only be enabled during developer testing of a module. 

    Do you think it is possible to programmatically call SOS to dump the references of a particular instance (!gcroot) and then process then result for the trace? 

    Wednesday, June 4, 2008 9:14 AM
  • I since settled for a little static class I called GCMon.  In the constructor of a class I want to monitor the collection of I put GCMon.AddWatch(this).  AddWatch is also has a number of other methods of the same name for extra things I want to trace like the parent, or to specify the ID reference field (Accessed by reflection at the time of tracing).  It works by a collection of WeakReference objects.

    When the program ends GCMon.TraceAlive() is called after GC.Collect and outputs a little report on what is still alive at the time the program ends.  Object type, when it was created, parent type, reference etc.

    It's still pretty rough and later I want to control the scope and report when manager class is closed if any of it's child items a still alive which should be possible.  It's at least something that should help us be better with out memory usage.

    Most of the code is behind #if DEBUG too so it wont slow down release.

    Tuesday, June 10, 2008 9:30 AM