3

I'm using Delphi 10.0 Seattle.

Suppose I have a record like this:

TmyRecord = record
  a,b : string;
  ar  : array of string
end;

And a variable like this:

v : array of TmyRecord;

and some code like this:

SetLength(v,2);
SetLength(v[0].ar,3);
SetLength(v[1].ar,2);
SetLength(v[0].ar[0],10);
SetLength(v[0].ar[1],5);
SetLength(v[0].ar[2],7);
...
v[0].ar[0][0] := 'aaaa';
v[0].ar[0][1] := 'bbbb';
....
v[1].ar[1][0] := 'xxxx';

Will this statement:

SetLength(v,0);

free all of the occupied memory, or do I have to free it manually?

1 Answer 1

6

Dynamic array memory is automatically managed by Delphi and is released when they go out of scope.

If you need to release array memory sooner you can explicitly clear the array v and that will automatically release all memory including the ones occupied by ar member of your record.

You don't have to do anything else.


There are several ways to clear the dynamic array in Delphi:

Setting array length to 0 is one way to clear the array.

SetLength(v,0);

You can also clear v array by setting it to nil

v := nil;

or by using the intrinsic Finalize:

Finalize(v);

All of these have identical meaning.

Sign up to request clarification or add additional context in comments.

1 Comment

Normally (e.g. if v is a local variable) I'd clear it by doing nothing (simply letting it fall out of scope).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.