object layout structure in .NET using disassembler

2019-05-07 14:40发布

问题:

I am interested in seeing the object layout structure, and am trying to use a disassembly in visual studio. Following is my code:

class myclass
{
  public int m_a;
}

myclass myc = new myclass();
myc.m_a = 23;
//I am setting a breakpoint after this line

I opened Memory1 window, and type myc in the Address field. I get the following details int the output window (used Windows XP PC 32bit with Intel compiler):

    0x0148B7BC  1c 93 a7 00 17 00 00 00 00 00 00 00 00 00 00 00 00 00 00

It appears that there is an additional pointer 00a7931c which is added in front of the object data, which increases the object size by 4 bytes. My confusion is that documentation says that object size is increase by 8 bytes due to header per object. Can someone please point me to where the other 4 bytes are?

回答1:

From Advanced .Net Debugging - CLR Object’s Internal Structure:

An object’s CLR internal structure is:

[DWORD: SyncBlock][DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…

Object Header: [DWORD: SyncBlock]
Object Pointer: [DWORD: MethodTable Pointer][DWORD: Reference type pointer]…[Value of Value Type field]…

Every Object is preceded by an ObjHeader (at a negative offset). The ObjHeader has an index to a SyncBlock.



回答2:

Take a look at 0x0148B7B8. An objects structure is:

SyncBlock (ptr size) MethodTable (ptr size) Fields...

The reference points to the method table, to allow for faster dereferencing (consider the relative frequency of virtual method and property calls vs. locking).