Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Memory management (VB6 SR6)

    Without diverting into the realm of pointers, has anyone got any good memory management tips where variables are concerned.

    Primarily my program is made of forms and standard modules, with both public and private functions.

    When closing a form, will unload frmname destroy all of the variables within the form? Likewise, at what point do private and public variables get detroyed in a module?
    Public is pretty obvious as it is available outside the module it is in, but do private variables get destroyed at some stage?

    The reason I ask is that I have a fairly substantial piece of code with a couple of setup forms which are ran if the setup file cannot be found, and practically none of this is used in the actual program. Currently, when the setup process is complete I use:
    Unload me ' for the setup form to be unloaded
    call Main 'For the program to be restarted and attempt to use the newly configured setup file.
    In creating the setup file I have used all sorts of code for listing SQL servers and databases within, file manipulation and a few other bits and pieces. Should I be unloading it all in some way?

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Memory management (VB6 SR6)

    As a matter of good programming practice I always include
    <code> set variable=Nothing</code>

    for every variable that I have created with
    <code> set variable=value</code>

    I think that private variables are supposed to be deleted when you exit from the context where they were created, but I have seen strange behaviour which makes me sceptical that this always happens.

    StuartR

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Memory management (VB6 SR6)

    I don't use Unload me in the form's code module, I use Me.Hide. This allows me to scavenge any values from its controls back in the module that called it. I then use both <code>Unload formname</code> and <code>Set formname = Nothing</code>. Not sure where I picked up that habit. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Memory management (VB6 SR6)

    Once a module-level variable (whether public or private) has been created, it will remain in memory as long as the application stays active.

    It is wise to set an object variable (the kind you assign a value with Set) to Nothing as soon as you don't need it any more. It will still exist, but all the memory used by its properties etc. will be released.

    For arrays, use Erase <arrayname> if you're done with them. For a static numeric array it won't help much (0 takes up as much space as any other number), but for a static string array, it will reset all strings to "". And the memory taken up by a dynamic array is completely released when you erase it.

    As Jefferson wrote, you can hide a form if still you want to refer to the values entered in its controls. Once you're finished with it, unload it - this will free up the memory used by the form.

  5. #5
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Memory management (VB6 SR6)

    Thanks for all of your replies.

    Most of my recent code that uses the Set method now follows with Set variablename = Nothing
    My string variables are now set to empty after use, and my arrays have been erased.

    Currently the end of my code usually looks like this:


    'Destroy variables
    set x = nothing
    Exit function

    Err:
    msgbox err.number & ": " & err.description
    set x = nothing
    end function

    However, in future functions I will use:

    exitF:
    set x = nothing
    Exit function

    err:
    msgbox err.number & ": " & err.description
    goto ExitF
    end function

    That way, if I have a huge list of variables to destroy, the code doesn't need to be repeated.

    Thanks again for your tips.

Posting Permissions

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