delphi - Why TRttiField.GetValue fails to fill result? -
i have complex nested record type. need monitor changes in values of it's fields after method call. used rtti (originally copied here):
procedure comparefields(const rec1, rec2: pointer; const recttypeinfo: trttitype; const basename: string; const baseoffset: integer; const lines: tstrings); var r1field, r2field: pointer; myfield: trttifield; state: string; path: string; begin if recttypeinfo.typekind = tkrecord begin myfield in recttypeinfo.getfields begin r1field := pointer(integer(rec1) + baseoffset + myfield.offset); r2field := pointer(integer(rec2) + baseoffset + myfield.offset); if comparemem(r1field, r2field, myfield.fieldtype.typesize) state := '==' else state := '<>'; path := basename + '.' + myfield.name; lines.add(format('%2s | %-20s | +%2d | %10s[%2d] | %22s | %22s', [state, path, baseoffset + myfield.offset, myfield.fieldtype.tostring, myfield.fieldtype.typesize, myfield.getvalue(r1field).tostring, myfield.getvalue(r2field).tostring])); if myfield.fieldtype.typekind = tkrecord comparefields(rec1, rec2, myfield.fieldtype, path, baseoffset + myfield.offset, lines); end; end; end;
the comparemem
part works fine , indicates fields have been changed. problem reporting values of tow record. myfield.getvalue(r1field).tostring
times return random value , that's because myfield.getvalue(r1field)
fill result random values. sample demonstrates problem:
type tmyrec1 = record f11: integer; f12: boolean; f13: double; end; tmyrec2 = record f21, f22: tmyrec1; f23: integer; f24: tmyrec1; end; procedure tform1.button1click(sender: tobject); var mycontext: trtticontext; rec1, rec2: tmyrec2; begin fillchar(rec1, sizeof(rec1), #0); fillchar(rec2, sizeof(rec1), #0); comparefields(@rec1, @rec2, mycontext.gettype(typeinfo(tmyrec2)), 'tmyrec2', 0, memo1.lines); end;
and result is:
== | tmyrec2.f21 | + 0 | tmyrec1[16] | (record) | (record) == | tmyrec2.f21.f11 | + 0 | integer[ 4] | 0 | 0 == | tmyrec2.f21.f12 | + 4 | boolean[ 1] | false | false == | tmyrec2.f21.f13 | + 8 | double[ 8] | 0 | 0 == | tmyrec2.f22 | +16 | tmyrec1[16] | (record) | (record) == | tmyrec2.f22.f11 | +16 | integer[ 4] | 0 | 0 == | tmyrec2.f22.f12 | +20 | boolean[ 1] | false | false == | tmyrec2.f22.f13 | +24 | double[ 8] | 0 | 0 == | tmyrec2.f23 | +32 | integer[ 4] | 21013568 | 0 <<-- == | tmyrec2.f24 | +40 | tmyrec1[16] | (record) | (record) == | tmyrec2.f24.f11 | +40 | integer[ 4] | 0 | 0 == | tmyrec2.f24.f12 | +44 | boolean[ 1] | false | false == | tmyrec2.f24.f13 | +48 | double[ 8] | 9.92338439963001e-303 | 0 <<--
as can see tmyrec2.f23
, tmyrec2.f24.f13
seem have values other '0' after calling fillchar
!
Comments
Post a Comment