XData return an List without freeing it

Hi,

i try to return an TObjectList with an Service. But i don't wan't the List (and all the Objects in the List) to be freed automatically.

At Programmstart i create a List. I wan't to keep this, but also be able to get the List via a Service

The return to the Client works well!

But then the List and all Objects (and subobjects) within are freed server side

I tryed using the TXDataOperationContext.Current.GetManager to solve this problem, like the documentation said. But still the same problem occurs.

Is there a way to handle this problem? Can i disable the automatic Memory-Managment completly or (better) for a service-call?

Here the simplified sourcecode.

This is the Service-Part

type
  [ServiceImplementation]
  TTeamService = class(TInterfacedObject, ITeamService)
  public
    function List: TTeamList;
  end;

implementation

function TTeamService.List: TTeamList;
var
  Context: TObjectManager;
begin
  Context := TXDataOperationContext.Current.GetManager;
  Context.OwnsObjects := False;
  Result := ServerContainer.Teams;
end;

and this is the ObjectList i want to return

unit clrTeam;

interface

uses
  System.Generics.Collections;

type
  TLeader = class(TObject)
  end;

  TTeam = class(TObject)
  strict private
    FLeader: TLeader;
  public
    constructor Create; reintroduce;
    destructor Destroy; override;
  end;

  TTeamList = class(TObjectList<TTeam>)
  end;

implementation

constructor TTeam.Create;
begin
  inherited Create;

  FLeader := TLeader.Create;
end;

destructor TTeam.Destroy;
begin
  FLeader.Free;

  inherited;
end;

end.

Creating the List and add one item

  FTeams := TTeamList.Create;
  FTeams.Add(TTeam.Create);

Hi @Ansgar_Gosling, welcome to TMS Support Center!

It's partially possible (see below)

This is a very peculiar situation, actually, because usually you should not use global/singleton data in the services, because they are multithreaded.

Thus, it will only work and make sense in the specific case where you have global data that is never modified. It might be your case, but then I would just suggest that you create a local copy of the data from the global one and return it.

What you can do to help in this task is to tell XData to destroy only the root object (the list, for example) and not the "inner" objects (the itens in list, for example).

You do that this way:

TXDataOperationContext.Current.Handler.ActionResultDestruction := TActionResultDestruction.RootOnly;

Reference: