Invalid Pointer Operation - Delphi

I added a TClientDataSet(cdsTemp) on my form screen, well, and I implement the fields of that TClientDataSet(cdsTemp) via code, like this:

for i := 0 to (cdsAux.Fields.Count - 1) do cdsTemp.Fields.Add(cdsAux.Fields[i]);

Notice that I'm playing the fields from cdsAux to cdsTemp, I don't know if it's the right way to do it, but it's the one I found, adapted and it's working.

Until there is everything right and working, however, when I run the application it passes the fields from cdsAux to the cdstemp of good, but when I close the form the error "Invalid Pointer Operation". If I comment on the procedure that is doing this procedure of throwing the cdsaux fields to the cdsTemp the error no longer happens.

I.e. the problem, apparently is there, I would like to know if I am forgetting something, if I am doing something wrong, if someone has been through it, etc.

Author: Eduardo Stefanello, 2017-02-21

1 answers

Create the fields as follows (where size is a variable integer):

for i := 0 to cdsAux.FieldCount -1 do
begin
  tamanho:= cdsAux.Fields[i].Size;
  if (cdsAux.Fields[i].DataType = ftString) and (tamanho = 0) then
    tamanho:=1
  else
    cdsTemp.FieldDefs.Add(cdsAux.Fields[i].FieldName,
          cdsAux.Fields[i].DataType, tamanho);
end;
 1
Author: Andrey, 2017-03-01 18:21:30