#1 2023-05-03 17:27:11

larand54
Member
Registered: 2018-12-25
Posts: 104

Invalid pointer operation when using IList with CreateAndFillPrepare

I get this error when the IList<TOrm> is zero referenced after using CreateAndFillPrepare.

// TOrmArticles is the same as IList<TArticle>
function TDHSSyncRepository.GetAllArticles(var aArts: TOrmArticles):TSyncRepoError;
var
  ormArt: TOrmArticle;
  i: integer;
begin
  result := srNotFound;
  i := 0;
//  result := Collections.NewList<TOrmArticle>; //Tested doing this call outside/inside this function but same result.
  ormArt := TOrmArticle.CreateAndFillPrepare(FRestOrm,'',[]);
  while ormArt.FillOne do begin
    aArts.Add(ormArt);
  end;
  result := srSuccess;
end;

When aArts no longer used  I get the error.
What's wrong here?


Delphi-11, WIN10

Offline

#2 2023-05-03 17:42:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: Invalid pointer operation when using IList with CreateAndFillPrepare

FillOne is ... filling one ormArt instance... (as its name states) wink
So you are adding the very same TOrmArticle  instance several times to the list.
Therefore, when you free the list you free the instance more than once. sad

So create a new TOrmArticle instance each time, or - even better - use the IList<> generics methods of FRestOrm. smile

Online

#3 2023-05-03 21:01:51

larand54
Member
Registered: 2018-12-25
Posts: 104

Re: Invalid pointer operation when using IList with CreateAndFillPrepare

Beutifull! Thanks!

This is the result:

function TDHSSyncRepository.GetAllArticles(var aArts: TOrmArticles):TSyncRepoError;
var
  ormArt: TOrmArticle;
begin
  result := srNotFound;
  FRestOrm.RetrieveIList(TOrmArticle, aArts);
  if aArts.Count < 1 then
    exit;
  result := srSuccess;
end;

Delphi-11, WIN10

Offline

Board footer

Powered by FluxBB