Date added/modified: 16 Feb 2001
It may be difficult to catch memory leaks. But, sometimes, memory leaks come into your program by faulty examples or even sample code which you never suspect! I got hit by a sample code in the FmxUtils unit which I happened to copy and use in my own code.
Here is a function in the FmxUtils supplied with Delphi 3 (corrected in Delphi 5 now):
function GetFileSize(const FileName: string): LongInt;
var
SearchRec: TSearchRec;
begin
if FindFirst(ExpandFileName(FileName), faAnyFile, SearchRec) = 0 then
Result := SearchRec.Size
else Result := -1;
end;
Can you spot the memory leak there? In Win32, a FindFirst call should always have a corresponding FindClose. Otherwise, it will cause a memory leak! If you have used this function, check everywhere. If you used the pair of FindFirst and FindNext in the traditional way in a loop, then you will most probably have a FindClose after the FindNext loop. No problem in that case. But, watch out for single use of FindFirst, used just to get file size or attributes.
A word of caution: Earlier, I was under the impression that calling FindClose even on failure of FindFirst doesn't cause any harm. This appears OK as far as my test on Windows 95 goes. But, according to Brad Stowers, it will lock up an NT application. So, it would be safer not to call FindClose on the failure of FindFirst.
See the fixed code for this in the sample code area.