0

I have an array like this:

type
  TSomeRecord = Packed Record
    field1: integer;
    field2: string[32];
    field3: boolean;
  End;
var
  SomeRecord: Array Of TSomeRecord;

On form creation I'm using SetLength and populating the SomeRecord with more than 1000 elements.

My question here: do I need to free SomeRecord on form destroy, or will Delphi take care of it automatically?

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • 2
    When you close an application, the OS frees all allocated memory automatically. So, no, it's not necessary to free the array. – Olivier Jul 25 '21 at 13:11
  • Dynamic arrays are managed type. That means their memory is automatically managed. Records are value types and unless you explicitly allocate their memory with GetMem or New you don't need to manage records either. – Dalija Prasnikar Jul 25 '21 at 13:30
  • 2
    Just to make Oliver's point a bit more clear: You are asking two different questions here. (1) Yes, Delphi dynamic arrays are reference counted and so automatically freed when no longer needed. During the runtime of your app, such arrays are allocated and freed as needed. (2) When the app is closed, typically you *don't need to* free *anything*, since the OS will take back all memory used by the app. Of course, you *can* still do that, but that's like cleaning your house immediately before it is demolished, as Raymond Chen once put it IIRC. – Andreas Rejbrand Jul 25 '21 at 13:40

0 Answers0