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?
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.