Help with leak

I am getting this error in one of my programs when I exit the program when running it from the IDE:

image

I only get this from one of my programs, so it is likely something I have done, but if you could point me in the right direction, I would very much appreciate it.

Delphi Berlin and Windows 10

I believe this is the same as Last version of RO/DA and memory leak?

Hi,

this memory leak can be appeared if you don’t clear Expression property, i.e.

table.DynamicWhere.Expression := expression1; //<<< memory leak
..
table.DynamicWhere.Expression := expression2;

correct usage: clear Expression before assigning a new one, i.e.

table.DynamicWhere.Expression := expression1;
..
table.DynamicWhere.Clear; // << removes memory leaks
table.DynamicWhere.Expression := expression2;

P.S. We don’t release Expression automatically because it can be used it in another expression, like

table.DynamicWhere.Expression :=
   table.DynamicWhere.NewBinaryExpression(
       table.DynamicWhere.Expression,
       expression1,
       dboAnd
   );

Ok, but it seams as though the example provided using the expression in another expression violates the proper usage you just said was necessary:

P.S. We don’t release Expression automatically because it can be used it in another expression, like

table.DynamicWhere.Expression :=
table.DynamicWhere.NewBinaryExpression(
table.DynamicWhere.Expression,
expression1,
dboAnd
);

Clear was not called, and the code would not work if you did clear the dynamic where. The third line would not have the starting expression because it would have been cleared.

If you must clear first to avoid a leak, why is that usage OK?

Hi,

table.DynamicWhere.Expression :=
   table.DynamicWhere.NewBinaryExpression(
      table.DynamicWhere.Expression,
      expression1,
      dboAnd
   );

lets to a bit rewrite it for better understanding:

old_expression := table.DynamicWhere.Expression;
new_expression :=  table.DynamicWhere.NewBinaryExpression(
      old_expression,
      another_expression,
      dboAnd
   );
table.DynamicWhere.Expression := new_expression;

here old_expression will be a part of new_expression so old_expression will be destroyed at destroying new_expression (i.e. table.DynamicWhere.Expression) automatically so no memory leaks will be generated.

table.DynamicWhere.Clear just calls FreeAndNil(Expression); and clears another internal variables.

Ok, I get it. Thanks.