Error 500 InvalidPointer at returning TList<T>

REST-Debugger said: 
{ "error": {
"code": "InvalidPointer",
"message": "Ungültige Zeigeroperation"
}}

I don't find the error.

Service-Function (Implementation-Unit)

type
  [ServiceImplementation]
  TPersonFunc = class(TInterfacedObject, IsngFunc, IPersonFunc)
  strict private  
    function SearchCount         ( const Param: TJSONObject) : TJSONObject; 
    function Search              ( const Param: TJSONObject) : TList<TPerson>;  
  end;
implementation
function TPersonFunc.Search( const Param: TJSONObject): TList<TPerson>;  
begin
      Result := UnitXY.Search( Filter_is_string)
end;


Then, the UnitXY

function Search(AFilter: String): TList<TPerson>;
var
  LSession: IDBSession;
  Criteria: TCriteria<TPerson>;
  Filter: TCustomCriterion;
begin
  LSession := TDBSession.Create( DBConnection);
  Criteria := LSession.objManager.Find<TPerson>;


    Criteria.Add( (Linq.Contains( 'Number', AFilter)) or
                      (Linq.Contains( 'Lastname', AFilter)) or
                      (Linq.Contains( 'Firstname', AFilter)) );


  Result := Criteria.List;
end;


I have removed unnecessary code. Usually the AFilter is assembled.
Therefore I don't use Fluent Design.

The error occurs after the last "end" in my programming. I see the error in the REST-Debugger.
If I debug the code, I see the position.

My result list has 8 objects.
In Unit Bcl.Json.ListConverter
In procedure TJsonValueListConverter.WriteJson(const Writer: TJsonWriter; const Value: TValue);
In line 119: ==> Item := List.GetItem(I);
the error happens on the 4. run. The first 3 are ok.

The Result-Set in the database is "normal

Have you any idea?

I have tested several other tables. And in every test is another error. Invalid handle / Cannot convert Pointer to JSON / and so on.


Is there a fundamental problem here?

XData Result as TList<T>.
http://www.tmssoftware.biz/business/xdata/doc/web/supported_types.html

Does TDBSession destroy objManager when it's destroyed? If yes, all the loaded entities (objects in the list) are also destroyed at the end of UnitXY.Search function.

YES,


grmmpf. I have searched the whole Day. 
Of course. This was the mistake. Shi...


Thank you. 

I take the Board in front of my head away. (German saying)

Do you have any suggestion, where I can destroy the object?
Is there an event or something else?

That's why XData provides the TXDataOperationContext.Current.Manager, it's a manager that is destroyed when the request is over.
Alternatively, you can also add extra objects to be destroyed when the request ends, using ManagedObjects set.
See section "Managed objects" in this topic:

http://www.tmssoftware.biz/business/xdata/doc/web/server_memory_management.html

In summary:
TXDataOperationContext.Current.Handler.ManagedObjects.Add(ObjectToBeDestroyed);

Wagner R. Landgraf2020-04-08 19:53:55