To get all objects from a specific type or any type from all managed heaps as an object[] array you only need these few lines of code.

private static void GetAllThreadObjects()
{
   using (var debugger = new MdbEng())
   {
      var dummy = new Thread(() => {});
      dummy.Name = "Dummy Thread";

      var threads = debugger.GetObjects("System.Threading.Thread", true);
               
      foreach (Thread t in threads)
      {
        Console.WriteLine("Managed thread {0} has Name {1}", t.ManagedThreadId, t.Name);
      }
      GC.KeepAlive(dummy);
   }
}

Managed thread 1 has Name
Managed thread 5 has Name Dummy Thread


This does open up a lot of interesting possibilities like patching live objects of an library to which you do not have the source code to and you cannot access the internal object model. Now you have all objects or of a specific types at your hand. This does make it especially easy to shoot yourself in the foot but this is used by WMemoryProfiler to enable the Instance Tracking feature which all commercial profilers do offer as well but the free ones normally do not have.

Last edited Jun 22, 2012 at 10:21 PM by Alois, version 4

Comments

No comments yet.