Destroy TList Memory

0

I created this object list with mormot and it does not let destroy the TList created after use:

type
   TTesteVO = class
private
  fcodigo    : Integer;
  fnome      : String;
  frespostas : String;
  ftotal     : Real;
published
  property codigo    : integer read fcodigo    write fcodigo;
  property nome      : string  read fnome      write fnome;
  property respostas : string  read frespostas write frespostas;
  property total     : real    read ftotal     write ftotal;
end;

var
  TesteVO   : TTesteVO;
  Lista     : TList;
  Itens     : Integer;
begin
  Lista  := TList.Create();
  for Itens := 1 to 10 do
  begin
    TesteVO := TTesteVO.Create;
    TesteVO.codigo := Itens;
    TesteVO.nome   := 'Nome: ' + IntToStr(Itens);
    TesteVO.total  := Itens * 10;
    Lista.Add(TesteVO);
  end;

MemoJson.Lines.Text := JSONReformat(ObjectToJSON(Lista));
for Itens := 0 to Lista.Count - 1 do
   TTesteVO(Lista.Items[Itens]).Free;

FreeAndNil(Lista);

Lista      := JSONToObjectList(TTesteVO, MemoJson.Lines.Text);

try
  for Itens := 0 to Lista.Count -1 do
    begin
      TesteVO := Lista[Itens];
    end;
finally
  for Itens := 0 to Lista.Count - 1 do
    TTesteVO(Lista.Items[Itens]).Free;
  FreeAndNil(Lista);
end;

end; end.

  

Error: "Invalid pointer operation" when running FreeAndNil (List);

    
asked by anonymous 16.11.2017 / 10:46

1 answer

0

Try this way

while Lista.Count <> 0 do
begin
  TesteVO := Lista[0];
  Lista.Delete[0];
  FreeAndNil(TesteVO)  
end;
FreeAndNil(Lista);
    
16.11.2017 / 13:02