procedure FinalizeInstance(p: Pointer; typeInfo: PTypeInfo);
var
ctx: TRttiContext;
f: TRttiField;
fieldType: TRttiType;
i: Integer;
arrayType: TRttiArrayType;
elementType: TRttiType;
obj: ^TObject;
begin
for f
in ctx.GetType(typeInfo).GetDeclaredFields
do
begin
fieldType := f.FieldType;
case fieldType.TypeKind
of
tkRecord:
begin
FinalizeInstance(Pointer(PByte(p) + f.Offset), fieldType.Handle);
FinalizeRecord(Pointer(PByte(p) + f.Offset), fieldType.Handle);
end;
tkArray:
begin
arrayType := TRttiArrayType(fieldType);
elementType := arrayType.ElementType;
// handle only one dim
for i := 0
to arrayType.TotalElementCount - 1
do
FinalizeInstance(Pointer(PByte(p) + f.Offset + (i * elementType.TypeSize)), elementType.Handle);
end;
tkClass:
begin
obj := Pointer(PByte(p) + f.Offset);
FreeAndNil(obj^);
end;
end;
end;
end;