Pergunta

I made a function that copies the Data from A TClientDataSet to B.

In production, the code will dynamically fill a TClientDataSet, like the following:

procedure LoadClientDataSet(const StringSql: String; ParamsAsArray: array of Variant; CdsToLoad: TClientDataSet);
begin
  //There is a shared TClientDataSet that retrieves 
  //all data from a TRemoteDataModule in here.

  //the following variables (and code) are here only to demonstration of the algorithm;
  GlobalMainThreadVariable.SharedClientDataSet.CommandText := StringSql;
  //Handle Parameters
  GlobalMainThreadVariable.SharedClientDataSet.Open;


  CdsToLoad.Data:= GlobalMainThreadVariable.SharedClientDataSet.Data;

  GlobalMainThreadVariable.SharedClientDataSet.Close; 
end;    

That said:

  • It is safe to do so? (By safe I mean, what kind of exceptions should I expect and how to deal with them?)
  • How do I release ".Data's" memory?
Foi útil?

Solução

The data storage residing behind the Data property is reference counted. Therefore you don't have to bother releasing it.

If you want to dive deeper into the intrinsics of TClientDataSet I recommend reading the really excellent book from Cary Jensen: Delphi in Depth: ClientDataSets

Outras dicas

By assigning the Data property like you did duplicates the records. You have now two different isntances of TClientDataset with two different set of records with precisely the same structure, same row count and same field values.

It´s safe to do that if the receiving TClientDataset does not have any field structure previously defined or the existing structure is compatible with the Data being assigned. However, if we are talking about a huge number of records, the assignment may take long time and, in an extreme circumstance, it could exaust the computer´s memory (it all the depends on the computer´s configuration).

In order to release the Data, just close the dataset.

If you prefer to have two instances of TClientDataset but one single instance of the records, my suggestion is to use the TClientDataset.CloneCursor method, that instead of copying the Data, just assign a reference to it in a different dataset. In this case it´s the very same Data shared between two different datasets.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top