What data does TObject contain?

TObject.InstanceSize returns 8, but TObject does not declare any data members. According to the implementation of TObject.ClassType, the first 4 bytes can be explained as a pointer to the metadata of the TClass object. Does anyone know why 4 more bytes of overhead are needed?

EDIT: Apparently, this is characteristic of D2009. In older versions, this is only 4 bytes.

+8
object delphi delphi-2009
source share
3 answers

In Delphi 2009, it is possible to have a link to a synchronization monitor . See:

class function TMonitor.GetFieldAddress(AObject: TObject): PPMonitor; class function TMonitor.GetMonitor(AObject: TObject): PMonitor; 

... in System.pas

There is also a pointer to VMT. (The table of virtual methods.) From Delphi in a nutshell:

The TObject class declares several methods and one special hidden field for storing a reference to an object class. This hidden field points to the class virtual method table (VMT). Each class has a unique VMT, and all objects of this class share the VMT class.

+12
source share

The object contains entries for all its fields, as well as additional space for storing a pointer to a table of virtual methods. VMT contains more than just virtual method pointers. I explain more about VMT on my website, including the diagram.

Delphi 2009 apparently introduces another hidden field in addition to the VMT pointer for storing the synchronization monitor. You can determine if it is added at the beginning or at the end of the class using 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 should be at the end of the object, because 4 only considers the size of the VMT pointer. If it prints the value 8, then the monitor field should be at the beginning, next to the VMT pointer.

I expect you to find the monitor at the beginning. Otherwise, this means that the layout of the descendant object is not just the layout of the base object with all the new fields added. This would mean that the offset of the monitor field depends on the type of runtime of the object, which complicates the implementation.

When a class implements an interface, the object layout includes more hidden fields. Fields contain pointers to the reference value of the object interface. When you have an IUnknown reference to an object, the pointer that it contains does not match the pointer to the VMT field of the object, which is what you have with a normal object reference. The value of the IUnknown pointer will be the address of the hidden field. I wrote more about the layout of classes that implement interfaces .

+3
source share

Just in case, if anyone is interested in why Craig Stuntz's answer was accepted, see his last comment on this answer:

It looks like it was added in D2009: http://blogs.embarcadero.com/abauer/2008/02/19/38856 See the links in this post for full details.

The link is no longer available, but the wayback-machine has it:

https://web.archive.org/web/20160409224957/blogs.embarcadero.com/abauer/2008/02/19/38856

0
source share

All Articles