TObject.InstanceSize returns 8, yet TObject doesn't declare any data members. According to the implementation of TObject.ClassType, the first 4 bytes can be explained as a pointer to the object's TClass metadata. Anyone know what the other 4 bytes of overhead are there for?
EDIT: Apparently this is specific to D2009. In older versions, it's only 4 bytes.
In Delphi 2009, there is the ability to have a reference to a synchronization monitor. See:
class function TMonitor.GetFieldAddress(AObject: TObject): PPMonitor;
class function TMonitor.GetMonitor(AObject: TObject): PMonitor;
...in System.pas
Also, there is still a pointer to the VMT. (Virtual Method Table.) From Delphi in a Nutshell:
The TObject class declares several
methods and one special, hidden field
to store a reference to the object's
class. This hidden field points to the
class's virtual method table (VMT).
Every class has a unique VMT and all
objects of that class share the
class's VMT.
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.
Just in case somebody is wondering why Craig Stuntz' answer was accpepted, see his last comment on that answer:
Looks like was added in D2009: http://blogs.embarcadero.com/abauer/2008/02/19/38856 See links in that post for full details.