Memory management while using PODO in service call parameters

Hi
I have xdata service method procedure DoSomething(saleData: tSaleData);
As far as In understand i don't need to free saleData
and I don't need to write destructor for TSaledata to free products and payments
But if I use tsaledata outside of Xdata (for example in DunitX tests), I would love to have destructror for Tsaledata and Tsaleproduct to destroy subclasses.
Is only option write destructor like this

  inherited;
  if (TXDataOperationContext.Current <>nil) and
     TXDataOperationContext.Current.Handler.ManagedObjects.Contains(self) then
    exit;
  Free child objects 

Sample classess

TSaleData = Class 
  products:tarray<tsaleproduct>;
  payments:tarray<tpayment>;
end;
Tsaleproduct = class
    productid: string;
   amount: double;
  discount:tDiscount;
end;
TDiscount = class
  ispercentage: Boolean;
  amount: Boolean;
end;
and so on ...

No need for that. Since XData 5.4 you can just add JsonManaged attributes to the DTO fields that the class will destroy itself. Then XData will not destroy it.
However, In your case, a TArray is not "known" by XData to be managed. If possible, just replace it by T TObjectList that owns the objects and you are safe:

TSaleData = Class 
  products:TObjectList<tsaleproduct>;
  payments:TObjectList<tpayment>;
  constructor Create;
  destructor Destroy; override;
end;

constructor TSaleData.Create;
begin
  inherited;
  payments := TObjectList<tpayment>.Create(True);
  products: TObjectList<tproducts>.Create(True);
end;

destructor TSaleData.Destroy;
begin
  payments.Free;
  products.Free;
  inherited;
end;

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.