Perhaps Memory Leak?


I have a service function in XData server with using Aurelius.

I have made 2 tests. One with TCriteria<T> and one without.

The Criteria-Version show a memory leak. See picture:

Here is the code

function TdmDatabasePerson.SearchCount(const AFilter: string): integer;
var
  Criteria: TCriteria<TPerson>;
  LSession: IDBSession;
begin
  LSession := TDBSession.Create( DBConnection);


  Criteria := LSession.objManager.Find<TPerson>;
  Criteria.Add( Linq.Contains( 'Firstname', AFilter));
  Result := Criteria.Select( Linq['PersonID'].Count).UniqueValue.Values[0];


end;


A "Criteria.Free" makes an AccessViolation
This Code produces no memory leak

function TdmDatabasePerson.SearchCount(const AFilter: string): integer;
var
  Criteria: TCriteria<TPerson>;
  LSession: IDBSession;
begin
  LSession := TDBSession.Create( DBConnection);


  Result := 8;


end;


Hi Thomas,

This is the correct code:



function TdmDatabasePerson.SearchCount(const AFilter: string): integer;
var
  Criteria: TCriteria<TPerson>;
  CriteriaResult: TCriteriaResult;
  LSession: IDBSession;
begin
  LSession := TDBSession.Create( DBConnection);


  Criteria := LSession.objManager.Find<TPerson>;
  Criteria.Add( Linq.Contains( 'Firstname', AFilter));
  CriteriaResult := Criteria.Select( Linq['PersonID'].Count).UniqueValue;
  try
    Result := CriteriaResult.Values[0];
  finally
    CriteriaResult.Free;
  end;
end;


More info here: https://download.tmssoftware.com/business/aurelius/doc/web/results_with_projections.html

Excerpt: 
"It's important to note that TCriteriaResult objects are not managed by the TObjectManager, so the retrieved objects must be destroyed. When using ListValues method to retrieve the results, the returned list is a TObjectList<T> object that already has its OwnsObjects property set to true. So destroyed the list should be enough"

Perfect!