How to reset a TCriteria

I want to clear the contents of a TCriteria. The UseCase is:

  Criteria.Add(ExpressionA and ExpressionB);
  DataBLS := Criteria.List;
  if DataBLS.Count = 0 then begin < ---- no records found, so clear the TCriteria and Restart
    Criteria.Clear; < ---- does not work 
     Criteria.Add(ExpressionA or ExpressionB);


Tx 
Bernd



You can't reuse a criteria that has been "open" (in this case, using List method). It will be destroyed at that point.

And if your intention is to clear the criteria, why don't you just create a new one?

This is, what I did. But this leads to memory leaks. Can I just continue with Criteria.Add?

Can you post the full code?

Can you provide the detailed memory leak info? What leaks, exactly?
Why do you mean "continue", if you are simply creating a new Criteria?
This is the code:


function TmodelRezeptSchnittstelleMain.SearchInBLS(sSearch: string; bRawMode: Boolean): TList<TTBLLEBENSMITTEL>;
var
  bFirst:      Boolean;
  Criteria:    TCriteria<TTBLLEBENSMITTEL>;
  ExpressionA: TLinqExpression;
  ExpressionB: TLinqExpression;
  i:           Integer;
  s:           string;
  t:           string;
begin
  FreeAndNil(DataBLS);
  Criteria := mgrManager.Find<TTBLLEBENSMITTEL>;
  bFirst := true;
  for s in sSearch.Split([' ']) do begin
    if bFirst then begin
      ExpressionA := Linq.like('Bezeichnung', '%' + s + '%');
      ExpressionB := Linq.like('Synonym', '%' + s + '%');
      bFirst := false;
    end else begin
      ExpressionA := ExpressionA and Linq.like('Bezeichnung', '%' + s + '%');
      ExpressionB := ExpressionB and Linq.like('Synonym', '%' + s + '%');
    end;
  end;
  Criteria.Add(ExpressionA or ExpressionB);
  DataBLS := Criteria.List;
  if DataBLS.Count = 0 then begin
//    Criteria := mgrManager.Find<TTBLLEBENSMITTEL>; < ------------------- with this line enabled, I get a memory leak
    bFirst := true;
    for s in sSearch.Split([' ']) do begin
      if bFirst then begin
        ExpressionA := Linq.like('Bezeichnung', '%' + s + '%');
        ExpressionB := Linq.like('Synonym', '%' + s + '%');
        bFirst := false;
      end else begin
        ExpressionA := ExpressionA or Linq.like('Bezeichnung', '%' + s + '%');
        ExpressionB := ExpressionB or Linq.like('Synonym', '%' + s + '%');
      end;
    end;
    Criteria.Add(ExpressionA or ExpressionB);
    DataBLS := Criteria.List;
  end;
  Result := DataBLS;
end;



This is the leak:


---------------------------
Unexpected Memory Leak
---------------------------
An unexpected memory leak has occurred. The unexpected small block leaks are:



45 - 52 bytes: TObjectList<entityLebensmittel.TTBLLEBENSMITTEL> x 1


---------------------------
OK   
---------------------------


Solved. 

 DataBLS := Criteria.List;
  if DataBLS.Count = 0 then begin
    FreeAndNil(DataBLS); < ----------------- this holds the list of objects and must be freed first.
    Criteria := mgrManager.Find<TTBLLEBENSMITTEL>;