Delphi – Is this Peter Below code be a memory leak?

I know I shouldn’t ask Peter Peter, but I found this code snippet while trying to debug some old D5 code Text File Size.

In my opinion, Sysutils.FindClose should be called the last line of the function. If the result is zero, is FindFirst established and hangs?

More questions: It shows that this is for text files, but can’t this be used for any file types? A better Delphi.FileSize(fname:string);?

function TextfileSize(const name: string): LongInt;
var
SRec: TSearchRec;
begin
if FindFirst( name, faAnyfile, SRec) = 0 then
begin
Result := SRec.Size;
Sysutils.FindClose(SRec);
end
else
Result := 0;
end;

It seems to me that the Sysutils.FindClose should be called as the last line of the function. If the Result is zero, doesn’t that leave the FindFirst established and hanging?

No. If FindFirst fails , You don’t need to call FindClose. For modern versions of Delphi, it can be safely called regardless of the result of FindFirst. For older versions of Delphi, the condition code in the question is required. Anyway, there is no leak here.

Unfortunately, the documentation does not make it clear. The only way to ensure this is to read the source code.

It states this is for Text files, but couldn’t ‘t this be used for any file type?

For all types of files, this function works exactly the same way.

< p>A better Delphi.FileSize(fname: string); ?

I don’t know what you mean Which function is it, but the function in your question has two disadvantages. First, it returns a 32-bit signed integer, so it provides error output for files larger than 2GB. Second, it reads the file size from the file system metadata, This may not always match the real file size. The latter issue may not be the issue you should be concerned about. It is usually reasonable to use the file size stored in the file system metadata.

p>

I know I shouldn’t ask Peter Peter, but I found this code snippet Text File Size when trying to debug some old D5 code.

In my opinion, it should Call Sysutils.FindClose the last line of the function. If the result is zero, is FindFirst established and hangs?

More questions: It shows that this is for text files, but can’t this be used for any file types? A better Delphi.FileSize(fname:string);?

function TextfileSize(const name: string): LongInt;
var
SRec: TSearchRec;
begin
if FindFirst( name, faAnyfile, SRec) = 0 then
begin
Result := SRec.Size;
Sysutils.FindClose(SRec);
end
else
Result := 0;
end;

It seems to me that the Sysutils.FindClose should be called as the last line of the function. If the Result is zero, doesn’t that leave the FindFirst established and hanging?

No. If FindFirst fails, you don’t need to call FindClose. For modern versions of Delphi , It can be safely called regardless of the result of FindFirst. For older versions of Delphi, the condition code in the question is required. Anyway, there is no leak here.

Unfortunately, the documentation does not make it clear. The only way to ensure this is to read the source code.

It states this is for Text files, but couldn’t this be used for any file type?

p>

For all types of files, this function runs in exactly the same way.

A better Delphi.FileSize(fname: string) ; ?

I don’t know which function you are referring to, but the function in your question has two disadvantages. First, it returns a 32-bit signed integer, so it is larger than 2GB The file provides error output. Secondly, it starts from the file system According to the file size read in, this may not always match the real file size. The latter issue may not be an issue you should be concerned about. It is usually reasonable to use the file size stored in the file system metadata.

Leave a Comment

Your email address will not be published.