An object contains entries for all its fields, plus extra space to hold a pointer to the virtual-method table. The VMT holds more than just virtual-method pointers. I explain more about the VMT at my Web site, including a diagram.
Apparently, Delphi 2009 introduces another hidden field in addition to the VMT pointer to hold the synchronization monitor. You can determine whether it is added at the beginning or at the end of the class with some simple code:
type
TTest = class
FField: Integer;
end;
var
obj: TTest;
ObjAddr, FieldAddr: Cardinal;
begin
Assert(TTest.InstanceSize = 12);
obj := TTest.Create;
ObjAddr := Cardinal(obj);
FieldAddr := Cardinal(@(obj.FField));
writeln(FieldAddr - ObjAddr);
end.
If it prints the value 4, then the monitor field must be at the end of the object because 4 only accounts for the size of the VMT pointer. If it prints the value 8, then the monitor field must be at the start, adjacent to the VMT pointer.
I expect you'll find the monitor at the start. Otherwise, it means that the layout of the descendant object isn't simply the layout of the base object with all the new fields appended. It would mean the offset of the monitor field depends on the run-time type of the object, and that makes the implementation more complicated.
When a class implements an interface, the object layout includes more hidden fields. The fields contain pointers to the object's interface-reference value. When you have an IUnknown
reference to an object, the pointer it holds isn't the same as the pointer to the object's VMT field, which is what you have with an ordinary object reference. The IUnknown
pointer value will be the address of the hidden field. I've written more about the layout of classes that implement interfaces.