You destructor is declared incorrectly. You wrote:
destructor Destroy();
But you must override the virtual destructor declared in TObject
. If you don't then your destructor will not be called by Free
which calls the virtual destructor declared in TObject
.
Fix it like this:
destructor Destroy(); override;
Although it doesn't matter in this case, you should get in to the habit of calling the inherited constructors and destructors in your constructors and destructors. That way, when you derive from a class that does more than TObject
does in its constructors and destructors, you will make sure the superclass code runs.
constructor tLeakClass.Create (AOwner : TComponent);
begin
inherited Create;
fsPrinter := TECR_Text.Create(AOwner);
end;
destructor tLeakClass.Destroy();
begin
fsPrinter.Free;
inherited;
end;
The FastMM report is a bit odd though. It reports that the TECR_Text
object is leaked. But since you created that as owned by the form, the form should take it down. The object that is clearly leaked in the code in the question is the instance of tLeakClass
So I suspect that there are other problems in the classes that we cannot see. Quite possibly you made the same error and omitted the override
on the destructors of the classes that we cannot see.