Show/Hide Toolbars

TMS XData Documentation

Navigation: TXDataClient

Client Memory Management

Scroll Prev Top Next More

Since method operations can deal with several types of objects, either Aurelius entities, plain old Delphi objects or even normal classes like TList<T> or TStream, it's important to know exactly how XData handles the lifetime of those objects, in order to avoid memory leaks or access violation exceptions due to releasing the same object multiple times.


This is the behavior or TXDataClient when it comes to receiving/sending objects (there is a separated topic for server-side memory management).


Any object sent to the server (passed as a parameter) is not destroyed. You must handle the lifetime of those objects yourself.

Any object of type TStream or TList<T> returned from the server is not destroyed. You must handle the lifetime of those objects yourself.

Any other object returned from the server which type is not the ones mentioned in the previous item is automatically destroyed by default.


So consider the example below:


  Customer: TCustomer;
  Invoices: TList<TInvoice>;

Invoices := Client.Service<ISomeService>.DoSomething(Customer);


Customer object is being passed as parameter. It will not be destroyed by the client and you must destroy it yourself. This is the same if you call Post, Put or Delete methods.


Items object (TList<T>) is being returned from the function. You must destroy the list yourself, it's not destroyed by the client. It's the same behavior for List method.


The TInvoice objects that are inside the list will be destroyed automatically by the client. You must not destroy them. Also, the same behavior for Get and List methods - entities are also destroyed by the client.


Alternatively, you can disable automatic management of entity instances at the client side, by using the ReturnedInstancesOwnership property:


Client.ReturnedInstancesOwnership := TInstanceOwnership.None;


The code above will prevent the client from destroying the object instances. You can also retrieve the list of all objects created by the client (that are supposed to be destroyed automatically) using property ReturnedEntities, in case you need to destroy them manually:


for Entity in Client.ReturnedEntities do {...}