I show a short design pattern for a user class in the code below.
type
MytestClass = class
alist: TStringlist;
public
constructor Create;
destructor destroy; override;
end;
{ MytestClass }
type
TForm1 = class(TForm)
btn_version01: TBitBtn;
btnversion02: TBitBtn;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure btn_version01Click(Sender: TObject);
procedure btnversion02Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
btestClass : MytestClass;
aComplexClassDesign : TComplexClassDesign;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor MytestClass.Create;
begin
alist := TStringlist.Create;
end;
destructor MytestClass.destroy;
begin
alist.free;
inherited;
end;
procedure TForm1.btnversion02Click(Sender: TObject);
var atestClass : MytestClass;
begin
///
atestClass :=MytestClass.Create;
atestClass.Free;
atestClass := nil;
end;
procedure TForm1.btn_version01Click(Sender: TObject);
var atestClass : MytestClass;
begin
///
atestClass :=MytestClass.Create;
atestClass.Free;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
btestClass.free;
aComplexClassDesign.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
btestClass :=MytestClass.Create;
aComplexClassDesign :=TComplexClassDesign.Create;
end;
end.
Hope this is a perfect design without Memory leaks and Access violations. All the classes I use in my real application are designed along this pattern.
FastMM4 does not show up any problem on my TComplexClassDesign in the code above. In the real application FASTMM4 is reporting a Memory leak for my TComplexClassDesign, even I call the free function in the Close Event of the form. If I step the through the code for sure this function is executed. Any idea how to debug this memory leak report, any option to see the instance of TComplexClassDesign which has not been released? Any other reason why I got this strange memory leak report ?
Bonus question :
DUnit always make teardown code like this
atestClass :=MytestClass.Create;
atestClass.Free;
atestClass := Nil
Is the last line of code really needed ?