Results 1 to 3 of 3

Thread: [RESOLVED] VB6: memory leak question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    22

    [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
    Last edited by nj2b; Jan 27th, 2009 at 12:57 PM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2007
    Posts
    22

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width