What data does a TObject contain?

a 夏天 提交于 2019-11-30 15:07:28

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.

Link no longer available, but the wayback-machine has it:

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



来源:https://stackoverflow.com/questions/679022/what-data-does-a-tobject-contain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!