Re: FindFirstFile Possible Memory Leak



Stefan Berglund wrote:
It strikes me that one should close every handle but I have an
example that does this:

Dim WFD As WIN32_FIND_DATA
Dim hFile As Long: hFile = FindFirstFile(sProgramFiles & "*.*", WFD)
If (hFile > 0) Then
Do
Dim lngEnd As Long: lngEnd = InStr(1, WFD.cFileName, Chr$(0))
Dim sName As String: sName = Trim$(Mid$(WFD.cFileName, 1, lngEnd - 1))
If (WFD.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY) Then
Dim sVersions As String: sVersions = sVersions & sName & "|"
End If
Loop Until FindNextFile(hFile, WFD) = 0
End If
Call FindClose(hFile)

Wouldn't it be more correct to close each handle inside the loop?

Is that not necessary or am I creating a memory leak?


A simple google on "FindNextFile" clears that up enough for my taste.

Since it requires a handle already initialized by FindFirstFile or
FindFirstFileEx, and also explicitly calls for closing the handle
afterward, I would say it implicitly promises to do things the right way.
Call it "handle recycling" if you like.


By the way, to limit how distracted people get here in the groups
by things irrelevant to your question, I *strongly* recommend you
rewrite the %$#^%$#^%ing colons out of your code before posting.




Bob
--
.



Relevant Pages

  • FindFirstFile Possible Memory Leak
    ... Dim WFD As WIN32_FIND_DATA ... If (hFile> 0) Then ... Dim lngEnd As Long: ... Wouldn't it be more correct to close each handle inside the loop? ...
    (microsoft.public.vb.general.discussion)
  • Re: FindFirstFile Possible Memory Leak
    ... Dim WFD As WIN32_FIND_DATA ... Dim lngEnd As Long: ... modified my habits from 80 column fixed width fonts from the sixty's to ... variable width fonts on large wide flat screens of the 21st century and ...
    (microsoft.public.vb.general.discussion)

Loading