Search code examples
delphidatasetmemory-safety

About Data from TClientDataSet


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?

Solution

  • 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