Destroy Tlist Memory

Asked

Viewed 243 times

0

I created this object list with mormot and it won’t let you 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);

  • my problem occurs in this section: List := Jsontoobjectlist(Ttestevo, Memojson.Lines.Text); Try for Items := 0 to List.Count -1 of Begin Test := List[Items]; end; Finally for Items := 0 to List.Count - 1 of Ttestevo(List.Items[Items]). Free; Freeandnil(List); end;

1 answer

0

Try it like this

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.