dimanche 2 janvier 2011

SOS.DLL, object Address and Visual Studio debugger


When we search leak memory in our application, we use SOS.dll in Windbg.exe or Visual studio IDE.

This tool is very very usefull but it is not easy to walk accross the references tree of object.

Visual Studio is more powerfull to do that. “Quick watch window” for example with its property explorator is perfect.

but “sos command’s” returns only addresses of objects …

Evaluate an object in VS from GCHandle address

!GCRoot give often this type of informations :

!GCRoot -nostacks 00c58918

If you evaluate this in VS debugger :

System.Runtime.InteropServices.GCHandle.FromIntPtr(new IntPtr(0x3011f8)).Target

You found the instance of System.Threading.Thread (address 00c21b30) !!

Find address object in Quick watch windows …

It is possible to fix the memory area of an object with System.Runtime.InteropServices.GCHandle object. When you create a GCHandle on a object with GCHandleType.Pinned option. GCHandle.AddrOfPinnedObject() return the memory location of it !

BUT it is possible to use internal method to give the current address of object even if this object is not pinned

If you evaluate this, in “Quick Watch window” of VS, or in “Immediat window” :

? string.Format("{0:x}",int.Parse(System.Runtime.InteropServices.GCHandle.InternalAddrOfPinnedObject(System.Runtime.InteropServices.GCHandle.Alloc(myObjectSomeWhere).GetHandleValue()).ToString())-4)

The result give the same address of myObjectSomeWhere in  SOS commands Like

  • !DumpObj
  • !GCRoot
  • !DumpHeap

If you know where the object is in your application, you can find the address of it, and compare with SOS Commands results.

Evaluate object in VS from directly address

It would be very usefull to evaluate, for example, 00d0b0dc EventHandler instance in VS debugger !

BUT I did not found a tips to do this … directly, sorry … :-(

This reference http://stackoverflow.com/questions/3141428/conversion-from-void-to-object-in-c asks the same question without answer …

If I would find it, I will be able to update this post !