unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
lazstringutils;
type
{ TForm1 }
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
procedure SplitStringTest1;
procedure SplitStringTest2;
procedure SplitStringTest3;
procedure SplitStringTest4;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
{ So.. SplitString() function which returns TStrings. How to use this correctly? }
const AStringOfWords='A String Of Space Delimted Words';
var TeeStrings:TStrings;
TeeStringList:TStringList;
procedure TForm1.SplitStringTest1; //NO Memory Leak! But is this the correct way?
var n:Integer;
begin
try
TeeStrings:=SplitString(AStringOfWords,' ');
for n:=0 to TeeStrings.Count-1 do
begin
Memo1.Lines.Add(TeeStrings[n]);
end;
finally
TeeStrings.Free; //Freed. But I did need to Create anything?
end;
end;
procedure TForm1.SplitStringTest2; //Memory Leak! But why is this not the correct way?
var n:Integer;
begin
try
TeeStrings:=TStrings.Create; // yep. It was created and...
TeeStrings:=SplitString(AStringOfWords,' ');
for n:=0 to TeeStrings.Count-1 do
begin
Memo1.Lines.Add(TeeStrings[n]);
end;
finally
TeeStrings.Free; //...it was freed
end;
{Heap dump by heaptrc unit of /home/sane/Desktop/USBDriveContent/SpoonFeed/SplitString/project1
967 memory blocks allocated : 1688995/1690376
966 memory blocks freed : 1688915/1690296
1 unfreed memory blocks : 80
True heap size : 1638400
True free heap : 1638112
Should be : 1638128
Call trace for block $00007FFFF6245C00 size 80
$00000000004642DA FORMCREATE, line 89 of unit1.pas
$000000000044EDF7 DOCREATE, line 939 of include/customform.inc
$000000000044CA6D AFTERCONSTRUCTION, line 149 of include/customform.inc
$00000000004555E4 CREATE, line 3184 of include/customform.inc
$000000000045F905 CREATEFORM, line 2239 of include/application.inc
$000000000041F066 main, line 38 of project1.lpr
$00000000004555B9 CREATE, line 3183 of include/customform.inc
$000000000045F905 CREATEFORM, line 2239 of include/application.inc
$000000000041F066 main, line 38 of project1.lpr}
end;
procedure TForm1.SplitStringTest3; //Memory Leak. TStringList and TStrings
var n:Integer;
begin
try
TeeStrings:=TStringList.Create; // Creating the TStringList
TeeStrings:=SplitString(AStringOfWords,' ');
for n:=0 to TeeStrings.Count-1 do
begin
Memo1.Lines.Add(TeeStrings[n]);
end;
finally
TeeStrings.Free;
end;
{Heap dump by heaptrc unit of /home/sane/Desktop/USBDriveContent/SpoonFeed/SplitString/project1
967 memory blocks allocated : 1689059/1690440
966 memory blocks freed : 1688915/1690296
1 unfreed memory blocks : 144
True heap size : 1638400
True free heap : 1638048
Should be : 1638064
Call trace for block $00007FFFF622CEC0 size 144
$00000000004642DA FORMCREATE, line 107 of unit1.pas
$000000000044EDF7 DOCREATE, line 939 of include/customform.inc
$000000000044CA6D AFTERCONSTRUCTION, line 149 of include/customform.inc
$00000000004555E4 CREATE, line 3184 of include/customform.inc
$000000000045F905 CREATEFORM, line 2239 of include/application.inc
$000000000041F066 main, line 38 of project1.lpr}
end;
procedure TForm1.SplitStringTest4; //This will NOT compile as SplitString result must be of TStrings
var n:Integer;
begin
try
TeeStringList:=TStringList.Create;
//TeeStringList:=SplitString(AStringOfWords,' '); //will NOT compile
for n:=0 to TeeStringList.Count-1 do
begin
Memo1.Lines.Add(TeeStringList[n]);
end;
finally
TeeStringList.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
SplitStringTest1;
SplitStringTest2;
SplitStringTest3;
SplitStringTest4;
end;
end.