Interesting question and interesting answer from Rob.
The followng demonstrates a quick and dirty technique to that some
might find useful when testing this stuff. I use D7.

Strangely, Rob's answer doesn't appear to be quite correct. Or am I
(again) doing somethin wrong:-(

-malcolm

procedure TForm1.Button1Click(Sender: TObject);
var s:string;
begin
 s:=Format('Start AllocMemCount: %9d',[AllocMemCount]);
 OutputDebugString(PChar(s));
 s:=Format('Start AllocMemSize: %10d',[AllocMemSize]);
 OutputDebugString(PChar(s));

 TestForLeak;

 s:=Format('End AllocMemCount: %9d',[AllocMemCount]);
 OutputDebugString(PChar(s));
 s:=Format('End AllocMemSize: %10d',[AllocMemSize]);
 OutputDebugString(PChar(s));
end;

procedure TForm1.TestForLeak;
type
 rRecord = record
   i : integer;
   str : string;
 end;
 pRecord = ^rRecord;
var
 i : integer;
 pnt : pRecord;
 list : Tlist;
begin
 list := Tlist.create;
 for i := 0 to 10 do
   begin
     new(pnt);
     pnt.i:= 1;
     pnt.str:=IntToStr(random(10));
     //pnt.str:='a';
     list.Add(pnt);
   end;

 for i := 0 to list.Count -1 do
   begin
     pnt:=list.Items[i];
     dispose(pnt);
     //dispose(list.Items[i]);
   end;
 list.free;
end;

(*
View/Debug Windows/Event Log shows following

With call to TestForLeak commented out:
ODS: Start AllocMemCount:       286
ODS: Start AllocMemSize:       8900
ODS: End AllocMemCount:       287
ODS: End AllocMemSize:       8900

TestForLeak uses
constant 'a' with dispose(list.Items[i]);
ODS: Start AllocMemCount:       286
ODS: Start AllocMemSize:       8900
ODS: End AllocMemCount:       298
ODS: End AllocMemSize:       9032

TestForLeak uses
random string with dispose(list.Items[i]);
ODS: Start AllocMemCount:       286
ODS: Start AllocMemSize:       8900
ODS: End AllocMemCount:       298
ODS: End AllocMemSize:       9032

TestForLeak uses
random string with dispose(pnt);
ODS: Start AllocMemCount:       286
ODS: Start AllocMemSize:       8900
ODS: End AllocMemCount:       287
ODS: End AllocMemSize:       8900

*)


__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to