Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi "Free" TStringDynArray

Tags:

delphi

Is there no need to free a TStringDynArray when created e.g. by SplitString? Attempts to free it fail as it is no object.

As I use it in a background process I am afraid that I create memory leaks by using it without explicitely freeing the memory.

like image 565
MichaSchumann Avatar asked Jul 19 '26 21:07

MichaSchumann


1 Answers

No, a dynamic array is managed by the compiler. It is reference counted and will be freed when the reference count drops to zero.

(However, if the elements of the array are (pointers to) objects, these objects will not be freed automatically. Only the array itself is freed. In your case, the elements are strings, and they are also managed by the compiler.)

But you may occasionally want to free the memory before the variables go out of scope. For instance, if you have a global variable which is a huge dynamic array, you can explicitly do a SetLength(MyArray, 0) or MyArray := nil or Finalize(MyArray) to let go of it.

like image 172
Andreas Rejbrand Avatar answered Jul 22 '26 05:07

Andreas Rejbrand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!