Overload functions FindAllFiles and FindAllDirectories with procedure returning the stringlist as a parameter
Original Reporter info from Mantis: wp @wpam
-
Reporter name:
Original Reporter info from Mantis: wp @wpam
- Reporter name:
Description:
The function FindAllFiles and FindAllDirectories return a stringlist as function result. This is a very risky construction because it invites for memory leaks.
For example, I found this in the forum:
Memo.Lines := FindAllFiles('.');
FindAllFiles creates the stringlist which is copied to the lines of the memo. There is no way to destroy the stringlist again. This result in a memory leak. Same with FindAllDirectories.
As a counter-measure I propose to overload these functions with procedures returning the stringlist as a parameter and mark the functions as deprecated:
procedure FindAllFiles(AList: TStringList; const SearchPath: String;
SearchMask: String = ''; SearchSubDirs: Boolean = True); overload;
procedure FindAllDiretories(AList: TStringList; const SearchPath: string;
SearchSubDirs: Boolean = True); overload;
With these proceduress it is necessary to expicitely create the stringlist before calling them, and it is absolutelely clear how to destroy them and who is responsible for that:
var
SL: TStringList;
begin
SL := TStringList.Create;
try
FindAllFiles(SL, '.');
Memo.Lines := SL;
finally
SL.Free;
end;
end;
More to type, but definitely faster than seeking the memory leak...
Or, with the same amount of typing:
FindAllFiles(Memo.Lines, '.');
Steps to reproduce:
I am adding a patch which overloads the functions FindAllFiles and FindAllDirectories according to the description above.
To demonstrate, run the attached demo containing the function and procedure versions. Uncomment the call to test. The demo activates heapttrc and shows that the procedures provided work correctly without a memory leak.