[RESOLVED] VB6: memory leak question
I have a routine in a module that accepts an array as a parameter and was wondering if the way I have it coded will cause memory leaks? As a side question, does VB have a way to tell if dynamic memory is being recovered?
Thanks in advance.
the definition
Code:
Public Sub dbExist(matches() As String, arrSize As Integer)
Dim spath As String, fname As String
Dim idx As Integer: idx = 0
spath = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
fname = Dir(spath, vbDirectory)
While fname <> ""
If fname <> "." And fname <> ".." Then
If (GetAttr(spath & fname) And vbDirectory) <> vbDirectory Then
If Right(LCase(fname), 3) = '".mdb" Then
ReDim Preserve matches(idx + 1)
matches(idx) = fname
idx = idx + 1
End If
End If
End If
fname = Dir
Wend
arrSize = idx
End Sub
The caller
Code:
Private Sub Form_Load()
Dim dbs() As String
Dim dsize As Integer
dbExist dbs, dsize
If dsize > 0 Then
dbFiles.Text = dbs(0)
For i = 0 To dsize - 1
dbFiles.AddItem dbs(i)
Next i
Erase dbs
End If
End Sub
Re: VB6: memory leak question
You are not going to leak memory by using standard VB functions, for the most part. Where people start leaking memory is when they use APIs and fail to destroy/delete what they created via those APIs. There are always exceptions though.
In your code, no memory leak can occur. However, you are duplicating efforts in your routines. I am assuming dbFiles is a combo or listbox. Why not populate that in your dbExist routine vs saving results to an array then looping thru the array to add to the combo/list box?
Re: [RESOLVED] VB6: memory leak question
I thought about using the array for holding full short path names to the files and use the combo box for the filename, but I may change that and use your suggestion.
LaVolpe, thanks for the feedback... I appreciate it.