When objects are freed, the Destroy destructor method is called.
Destroy must be an override (or virtual) method.
All classes inherit from TObject, which declares a virtual Destroy
method, and should override this method to provide their own implementation.
Not naming the destructor Destroy will cause it not to be called when the object
is freed. The same thing happens if Destroy is not an override, because
the destructor is invoked polymorphically when the object is freed.
Ensure the destructor is named Destroy and marked with the override
directive.
type
TNode = class(TObject)
private
FChildNode: TNode;
public
destructor CustomDestroy;
end;
destructor TNode.CustomDestroy;
begin
FChildNode.Free;
end;
type
TNode = class(TObject)
private
FChildNode: TNode;
public
destructor Destroy; override;
end;
destructor TNode.Destroy;
begin
FChildNode.Free;
inherited;
end;
Note that in the "before" case above, FChildNode.Free would not call the destructor
on TNode, causing a memory leak.