Trying to find the roots of objects using CLR MD

Here is my class

namespace MyNamespace
{
    public class MyClass
    {
        private byte[] imageBytes = null;

        public MyClass() { }

        public void LoadImage(string filePath)
        {
            Image img = Image.FromFile(filePath);
            using (MemoryStream mStream = new MemoryStream())
            {
                img.Save(mStream, img.RawFormat);
                imageBytes  = mStream.ToArray();
            }
        }

        public void RemoveImage()
        {
            imageBytes = null;
        }
    }
}

And that's how it was used

static void Main(string[] args)
    {
        MyClass mc = new MyClass();

        mc.LoadImage(@"C:\Images\myImage.jpg");

        Console.WriteLine("take process dump now...");
        Console.Read();
        mc.RemoveImage();
    }

I run the program and take a snapshot of the process. Not surprisingly, this is what I found about MyClass links.

0:000> !DumpHeap -type MyClass
         Address               MT     Size
0000000002b92e08 000007fe73423a20       24     

Statistics:
              MT    Count    TotalSize Class Name
000007fe73423a20        1           24 MyNamespace.MyClass
Total 1 objects
0:000> !GCRoot 0000000002b92e08 
Thread 3b3c:
    00000000004eef90 000007fe7354011f MyTestApp.Program.Main(System.String[]) [c:\Projects\MyTestApp\Program.cs @ 17]
        caller.rsp-30: 00000000004eefb0
            ->  0000000002b92e08 MyNamespace.MyClass

Found 1 unique roots (run '!GCRoot -all' to see all roots).

Now I would like to see if I can get the same roots for the MyClass instance that is present in the same dump file using the CLR MD. For this, I use the GCRoot sample. One of the inputs to this application is ulong obj . I'm not sure how to get this for an instance of MyClass, so I did this in the following code in the main GCRoot sample method.

foreach (ulong obj2 in heap.EnumerateObjects())
{
   ClrType type2 = heap.GetObjectType(obj2);
   if (type2.Name.StartsWith("MyNamespace.MyClass") )
       obj = obj2;
}

Thus, I see that obj is getting a valid value, but the problem is that the next line of code does not find node, as it always returns NULL.

Node path = FindPathToTarget(heap, root);

, MyClass . .

+4
1

ClrMD , GCRoot, GCRoot , windbg! GCRoot.

"MyClass" ( var ), .

ClrRoot, Heap.EnumerateRoots(), "MyClass".

+4

All Articles