I have a small test program. When I look into stack object for main thread, its showing MyClass twice there. Any ideas why there are two object of MyClass on the stack?
class Program
{
struct MyStruct
{
int x;
int y;
}
class MyClass
{
int x;
int y;
}
static void Main(string[] args)
{
MyStruct s ;
MyClass c = new MyClass();
}
}
0:000> !DumpStackObjects
OS Thread Id: 0xf74 (0)
RSP/REG Object Name
000000000023e9e8 00000000028f3c90 ConsoleApplication2.Program+MyClass
000000000023e9f8 00000000028f3c90 ConsoleApplication2.Program+MyClass
000000000023ea10 00000000028f3c70 System.Object[] (System.String[])
000000000023eb98 00000000028f3c70 System.Object[] (System.String[])
000000000023ed80 00000000028f3c70 System.Object[] (System.String[])
000000000023eda8 00000000028f3c70 System.Object[] (System.String[])
Actually it shows that the one and only instance is referenced twice. Notice the values in the leftmost column differ. It could be different registers or different parts of the stack frame which point to the same instance.
In my experience this happens quite often (especially with debug builds).
!dsois useful for locating objects and in that case the important column is the Object column, which holds the actual references.E.g. if I run your example above from the debugger and place a break point on Main the output from
dsolooks like this just before the Main method returns.As you can see both the
eaxand theecxregisters hold a reference to the instance despite the fact that the C# source only has a single reference.If you look at the JIT compiled code for Main it looks like this
Notice the instruction
call 00142020. This creates the instance ofMyClassand returns the reference in theeaxregister. Following that the reference is stored atdword ptr [ebp-14h].The next instruction reads the value stored at
dword ptr [ebp-14h]and stores the value in theecxregister, which is then used as input for the call to the constructorcall dword ptr ds:[15398Ch] (TestApp.Program+MyClass..ctor(), mdToken: 06000003).This explains why
dsolists the reference twice in this case. However, you rarely need to go into the details about this when debugging.