Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindClose in delphi

Tags:

delphi

I have witnessed a Delphi 6 program that uses FindFirst() from SysUtils inside a function. This is a program that runs for months and performs this operation hundreds of times. The surprising thing is that FindClose() is not called and nothing bad happens.

If I understand that correctly, the program seems to leak file Handles. (In the TSearchRec record there is a THandle entry. The whole record is discarded and the THandle not closed). The thing is that there seems to be no problem. I used process explorer form sysinternal and observed no open file handle to the file found by FindFirst.

Any explanation?

like image 825
Spyros Komninos Avatar asked Jan 07 '23 07:01

Spyros Komninos


1 Answers

As described, your program leaks handles. If you call FindFirst without subsequently calling FindClose, you leak handles. Do that enough and the program will exhaust the available handle objects and fail.

That you don't see that suggests that you have not yet reached the limit. That you do not observe the leak from Process Explorer suggests that you are not looking at the right information. Or perhaps you have mis-diagnosed the problem completely and either the code that leaks is not called, or the call to FindClose is made but you have not found it yet.

Rather than spending much more time analysing this, you should fix your code. The bottom line is that unless you match calls to FindFirst with calls to FindClose, your program leaks.

like image 144
David Heffernan Avatar answered Jan 14 '23 03:01

David Heffernan