Don't understand completely what is going on, but when you have a look at for example IsPublishedProp
in the TypInfo
unit, you'll see that it casts the ClassInfo of the instance as a pointer to a TypeInfo structure:
PTypeInfo(Instance.ClassInfo)
When you look at the ClassInfo method, it returns a simple pointer the value of which seems related to the vmt table:
Result := PPointer(Integer(Self) + vmtTypeInfo)^;
vmtTypeInfo
has a value of -72. Four bytes before that at -76 is vmtInitTable
. vmtTypeInfo is followed by FieldTable, MethodTable, DynamicTable etc.
the vmtInitTable value is used in for example TObject.CleanupInstance
and passed to _FinalizeRecord
as the pointer to the TypeInfo structure.
So the four bytes before the TypeInfo structure pointing to the TypeInfo structure seem to be there by design and part of the vmt structure.
Edit
As Mason rightly pointed out the above is a complete red herring (see comments). I am leaving the answer so others won't have to chase it down.
Update
To avoid confusion over variables and their addresses, I have rewritten Mason's test procedure as follows:
procedure test(info: PTypeInfo);
begin
writeln('value of info : ', cardinal(info));
writeln('info - 4 : ', cardinal(info) - 4);
writeln('value 4 bytes before: ', cardinal(PPointer(cardinal(info)-4)^));
end;
and call it with the following information:
procedure TryRTTIStuff;
begin
writeln('TPersistent');
test(TypeInfo(TPersistent));
writeln('TTypeKind enumeration');
test(TypeInfo(TTypeKind));
writeln('Integer');
test(TypeInfo(Integer));
writeln('Nonsense');
test(PTypeInfo($420000));
end;
The first three produce the results that Mason describes. I only added an extra writeln to show the pointer value for the last writeln. The last call in TryRTTIStuff is to show that when you do not pass in a pointer to a valid TypeInfo structure, you do not get the same value on the first and third writeln's for the call.
No clue yet as to what is going on with the TypeInfo. Maybe we should ask Barry Kelly as he is the author of the new D2010 RTTI so should know a lot about the old one as well...