Destructing object whose private variables are of dynamic types lazarus / 61-Beginners ---------------------------------------------------------------------- AlexanderK · 2025-06-12 17:01 Hello! A newbie here. This is a simplified example of a class taken from a book from 2013. 'TPerson' class has two private variables 'FAwards' of array type and 'FName' of 'ansistring' type (because of '{$H+}'). Both types are considered dynamic and managed on the heap. Now, in the original code of the class the implementation of the destructor was without 'SetLength(FName,0);'. To my understanding, both  'FAwards' and 'FName' are dynamic and managed on the heap and both should be released upon destruction. According to 'SetLength' documentation, "If Zero is specified, the array is cleared and removed from memory." Should both types be set to zero to properly destruct the instance of the class or 'ansistring' types could be skipped? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program person_class; {$mode objfpc}{$H+} type  TPerson = class    FAwards: array of string;    FName: string;     constructor create(aName: string);    destructor destroy; override;    procedure displayInfo;   property name: string read FName;  end;  constructor TPerson.create(aName: string);  begin    inherited Create;    FName := aName;  end; destructor TPerson.destroy;  begin    SetLength(FAwards,0);    SetLength(FName,0); // Should be set to zero just as 'FAwards'?    inherited Destroy;  end; procedure TPerson.displayInfo;  begin    WriteLn('Person name: ',name);  end; var person: TPerson; begin  person:= TPerson.create('John Smith');  person.displayInfo;  person.Free;   {$IfDef WINDOWS}  ReadLn;  {$EndIf}end. ---------------------------------------------------------------------- Martin_fr · 2025-06-12 18:02 There may be a debate on "coding style" or even personal preference.... Going for technical only: You don't need to set them to length zero. This will be done. You can compile your app with Heaptrc -gh (on the debugger page of project options), and then when it ends it will give you a message. Part of that message should be "0 unfreed blocks" (or very similar) Note, if you use "pointer to records" and then allocate mem for them, this may differ. IIRC (but need to check) dispose, should deal with it, but if you use AllocMem and FreeMem directly, and then assign the memory to a pointer to record, containing an ansistring => then that string is not taken care off. ---------------------------------------------------------------------- AlexanderK · 2025-06-12 19:11 In relation to 'Heaptrc -gh'. I ticked the option but the only output I see in the console window is the expected result from of the program. Attached the screenshots with the output and Debugging settings. Do I miss something? ---------------------------------------------------------------------- Martin_fr · 2025-06-12 19:24 I don't see a prompt on your console, so I guess the program is still running? Heaptrc only reports when the program exits for good. It should then print Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Heap dump by heaptrc unit of "project1.exe"0 memory blocks allocated : 0/00 memory blocks freed     : 0/00 unfreed memory blocks : 0True heap size : 65536 (192 used in System startup)True free heap : 65344 The non-zero numbers can vary. The "unfreed" should be zero. ---------------------------------------------------------------------- AlexanderK · 2025-06-12 19:46 You're right. The program is still running. In the screenshot I attached before, the program stopped after {$IfDef WINDOWS} at ReadLn and awaits [Enter] key to finish, but when I press [Enter] the window closes so I'm unable to see the heaptrc report. On Windows I have to add {$IfDef WINDOWS} ReadLn; {$EndIf} at the end. Otherwise, the console window immediately closes after execution. Any remedy to this? Maybe the report is being saved to a file somewhere? P.S. Understood about the heaptrc report. Thank you! ---------------------------------------------------------------------- lainz · 2025-06-12 19:52 Run a cmd Window. (Windows Key + R, type cmd). Go to the folder where your exe is. CD "C:\mydir" And run from there, the command window will not close. ---------------------------------------------------------------------- Martin_fr · 2025-06-12 20:10 https://www.freepascal.org/docs-html/rtl/heaptrc/environment.html ---------------------------------------------------------------------- AlexanderK · 2025-06-12 20:21 Running it from within cmd works. I see the report now. Appreciate your help. Thank you! P.S. Noted about the environment link.