How to free objects in entity list obtained by TXDataClient.List<T> call and modified by TAureliusDataset

Most probably I am missing something obvious, but when I implemented the following scenario, I ran into a problem:

  1. I obtain entity list via REST call TXdataClient.List
  2. I modify the list by using TAureliusDataset (to which I provide this list as sourcelist). The important thing is that new records are added by Insert
  3. The objects added by using TAureliusDataset are not managed by TAureliusDataset and are added to the sourcelist automatically by TAureliusDataset itself.
  4. I free the list. Objects that came from the original TXDataClient.List<> call are fred automatically, Objects added by TAureliusDataset hang in the list causing MemoryLeak.
    What to do? What is the best way to cope with this scenario?

Use event OnObjectInsert of the dataset. That's the point where you should save a reference to the object to further destroy it. You can do any way you want, but the most straightforward way to do so is just let the TXDataClient do it for you:

procedure TMyForm.AureliusDataset1ObjectInsert(Dataset: TDataset; AObject: TObject);
begin
  // Your original code, if any. Probably calling XDataClient.Post(AObject)
  XDataClient.ReturnedEntities.Add(AObject); 
end;

Thank you for the pointer. First I thought that there should be some property of the AureliusDataset that solves this, and, having found the Manager property, I investigated this path - to no avail. So since I already use OnObjectInsert for POSTing the data on the REST server, I realized that I probably have to solve the problem "manually" by creating the list of all inserted objects in the app that should be eventually destroyed and add the object there from OnObjectInsert. Now from your post I learned that there already is such list in the box - XDataClient.ReturnedEntities - that does it for me. The only thing that worries me is that the newly created object actually is not a returned entity, so is there any danger that TXDataClient gets confused in some future version? Anyway, I shall use your suggestion from now on.

No. The name ReturnedEntities is actually even misleading, that property holds all the objects that will be destroyed by the client.