[Object types, VMT] no way to determine VMT pointer offset, incorrect object size

Original Reporter info from Mantis: DragoN
  • Reporter name: Paul

Description:

Using: FPC, Win32, Delphi-compatibility mode.

  1. Cannot determine VMT pointer offset in object types (not classes).

In Delphi, it was a field in VMT table, VMTOffset (address = VMT - 12). It describes offset from start of object data to VMT pointer. In FPC, I cannot find how to determine this value. FPC VMT consists only of instance size and parent VMT pointer.

I think, it must be some way to determine VMT offset.
In Delphi7 it was a constant ovtVMTPtrOffset = -12. So address (VMT - 12) was filled with VMT pointer offset, relative to object first byte address.

  1. VMT pointer placed unaligned.

When SizeOf(Object without virtual methods) not aligned by 4-byte boundary, VMT pointer is placed right after end of object data, unaligned. (packed keyword not used)

  1. Incorrect object size.

When VMT pointer is placed unaligned, SizeOf(Object with virtual methods) becomes 4-byte aligned. So end of object data is never used. It seems that aligning of object size was maked to align VMT, but...

Sample (Object size = 8 bytes): [*][V][M][T][][?][?][?].
* - Object data (1 byte)
VMT
- Pointer to VMT (4 byte)
??? - Waste.

  1. Documentation mistake.

http://www.freepascal.org/docs-html/prog/progsu151.html

Accordingly this, VMT pointer must be placed before all other object data (and aligned). But in fact, VMT is placed after all object's data (like in Delphi), and unaligned.

Steps to reproduce:

Declare object type with several fields and virtual methods, and analyze VMT pointer offset and size of object instance. Vary size of object data (not aligning it), and see how VMT pointer will be moved.

Additional information:

It would be nice, if problem #1 or #2 will be solved.
Some test code in attach (I was playing with objects, SizeOf(), vmt offset).

Mantis conversion info:

  • Mantis ID: 16034
  • OS: Windows
  • OS Build: 2000 SP4
  • Platform: Intel x86
  • Version: 2.4.0
  • Fixed in version: 2.6.0
  • Fixed in revision: 16321 (#dd886442)
  • Monitored by: » alexionne (Aleksa Todorovic), » DragoN (Paul)