× General discussions, feature requests for CodeTyphon Project and discussions that don't fit in any of the other specific CodeTyphon forum categories.

Important Memory leak in fileutil.inc

  • Kristof Subryan
  • Topic Author
  • Offline
  • New Member
  • New Member
More
4 months 2 weeks ago #16714 by Kristof Subryan
Memory leak in fileutil.inc was created by Kristof Subryan
// Apology if I am posting into incorrect forum.
// There is a memory leak in fileutil.inc
// the leak is marked with '<
THIS LINE NEEDS TO BE REMOVED!' in the code below

procedure TFileSearcher.Search(const ASearchPath: String; const ASearchMask: String;
ASearchSubDirs: Boolean; CaseSensitive: Boolean = False);
var
MaskList: TMaskList;
SearchDirectories: TStringList;

procedure DoSearch(const APath: String; const ALevel: Integer);
var
P: String;
PathInfo: TSearchRec;
begin
P := APath + AllDirectoryEntriesMask;

if FindFirstUTF8(P, FileAttribute, PathInfo) = 0 then
try
repeat
// skip special files
if (PathInfo.Name = '.') or (PathInfo.Name = '..') or
(PathInfo.Name = '') then Continue;
// Deal with both files and directories
if (PathInfo.Attr and faDirectory) = 0 then
begin // File
if (MaskList = nil) or MaskList.Matches(PathInfo.Name) then
begin
FPath := APath;
FLevel := ALevel;
FFileInfo := PathInfo;
DoFileFound;
end;
end
else begin // Directory
FPath := APath;
FLevel := ALevel;
FFileInfo := PathInfo;
DoDirectoryFound;
end;

until (FindNextUTF8(PathInfo) <> 0) or not FSearching;
finally
FindCloseUTF8(PathInfo);
end;

if ASearchSubDirs or (ALevel > 0) then
// search recursively in directories
if FindFirstUTF8(P, DirectoryAttribute, PathInfo) = 0 then
try
repeat
if (PathInfo.Name = '.') or (PathInfo.Name = '..') or
(PathInfo.Name = '') or ((PathInfo.Attr and faDirectory) = 0) or
(not FFollowSymLink and FileIsSymlink(APath + PathInfo.Name))
then Continue;

FPath := APath;
FLevel := ALevel;
FFileInfo := PathInfo;
DoDirectoryEnter;
if not FSearching then Break;

DoSearch(AppendPathDelim(APath + PathInfo.Name), Succ(ALevel));

until (FindNextUTF8(PathInfo) <> 0);
finally
FindCloseUTF8(PathInfo);
end;
end;

var
p1, p2: SizeInt;
i: Integer;
Dir: String;
OtherDir: String;
begin
if FSearching then RaiseSearchingError;
{$ifdef windows}
MaskList := TWindowsMaskList.Create(ASearchMask, FMaskSeparator, CaseSensitive);
{$else}
MaskList := TMaskList.Create(ASearchMask, FMaskSeparator, CaseSensitive);
{$endif}
MaskList := TMaskList.Create(ASearchMask, FMaskSeparator, CaseSensitive); <
THIS LINE NEEDS TO BE REMOVED!
// empty mask = all files mask
if MaskList.Count = 0 then
FreeAndNil(MaskList);

FSearching := True;
SearchDirectories:=TStringList.Create;
try
p1:=1;
while p1<=Length(ASearchPath) do
begin
p2:=PosEx(FPathSeparator,ASearchPath,p1);
if p2<1 then
p2:=length(ASearchPath)+1;
Dir:=ResolveDots(Copy(ASearchPath,p1,p2-p1));
p1:=p2+1;
if Dir='' then continue;
Dir:=ChompPathDelim(Dir);
for i:=SearchDirectories.Count-1 downto 0 do
begin
OtherDir:=SearchDirectories;
if (CompareFilenames(Dir,OtherDir)=0)
or (ASearchSubDirs and (FileIsInPath(Dir,OtherDir))) then
begin
// directory Dir is already searched
Dir:='';
break;
end;
if ASearchSubDirs and FileIsInPath(OtherDir,Dir) then
// directory Dir includes the old directory => delete
SearchDirectories.Delete(i);
end;
if Dir<>'' then
SearchDirectories.Add(Dir);
end;
//Search currentdirectory if ASearchPath = ''
if (SearchDirectories.Count=0) then
DoSearch('',0)
else
begin
for i:=0 to SearchDirectories.Count-1 do
DoSearch(AppendPathDelim(SearchDirectories), 0);
end;
finally
SearchDirectories.Free;
FSearching := False;
MaskList.Free;
end;
end;

Please Log in or Create an account to join the conversation.