Anything created or allocated must be manually freed; objects, arrays, arrays of objects, and records (especially if they contain the previous) for good form.
Code:
type
TThingy = class
private
fArray: array of integer;
procedure DoSomethingIrritating;
public
destructor Destroy; override;
constructor Create;
end;
{...}
destructor TThingy.Destroy;
begin
setlength(fArray,0);
inherited Destroy;
end;
constructor TThingy.Create;
var i: integer;
begin
setlength(fArray,100);
for i := 0 to 100 do
fArray[i] := random(1000000);
end;
procedure TThingy.DoSomethingIrritating;
begin
for i := 0 to 5 do
if random() > 0.5 then
fArray[high(fArray)] := pwr(random(90)+10,random(6)+1);
if random() > 0.3 then
DoSomethingIrritating;
end;
Don't override MyObject.Free ever. Only override the destroy destructor and Delphi/FPC/Lazarus will do the rest. The point is, just override the descendant function and then call the parent one. Free isn't a function intended to be overridden, and was written to do a few things and then call the destroy event.
In your code you should call free, but in designing an object you should override destroy. Any further questions?
(This is pseudocode written in the browser, so don't blame me if it doesn't work. I haven't touched Delphi in 45 days.)
Bookmarks