Jump to content
Jacek Laskowski

Issue with variant in record in array

Recommended Posts

Simple example:

 

program leak;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  FastMM4,
  System.SysUtils;


type
 TRx = record
        s : string;
        v : Variant;
      end;

 TAx = TArray<TRx>;

procedure Test;
var
  lA : TAx;
begin
  SetLength(lA, 3);
  lA[0].s := 'test2';
  lA[0].v := 888;
  lA[1].s := 'test1';
  lA[1].v := Now;
  lA[2].s := 'test3';
  lA[2].v := 'str';
end;

begin
  try
    Test;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

When I don't assign TDateTime to a Variant field, the debugger shows the array and all records correctly, as in the picture on the left. And there is no leakage.

 

vps_2020_04.28_07.thumb.png.4d3bbb901768559106d03be1d890109f.png

 

But when I assign TDateTime type to Variant field, as on the right side of the picture, the debugger does not show "plus" in hint window and I get a memory leak at the end.

 

What is wrong with this code?

 

 

Share this post


Link to post

Do you get the same leak report if you don't debug the code, just let it to run?

It should not be the case, but try to call VarClear on the variant before exiting, and finalizing the array.

Share this post


Link to post

In fact, when I'm not debuging, there is no leak.
But is it an IDE error to show the record array (without the record expansion plus) in a hint debugger? Because I was afraid it was a problem in my record and array memory management. I do some tricks there.

Share this post


Link to post

When the debugger evaluates some variables to display values in the debugger it often leaks memory. This is a defect with the debugger. The code here does not leak. Any reported leaks are due the debugger. 

 

I see this very often with property getter functions that return strings. The debugger calls the function, creating a new string. But then the debugger fails to destroy the string. Leak. 

Share this post


Link to post
11 hours ago, aehimself said:

Do you get the same leak report if you don't debug the code, just let it to run?

It should not be the case, but try to call VarClear on the variant before exiting, and finalizing the array.

Not needed. Really not a great idea to suggest needless actions with no basis in fact. Just spreads FUD. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×