CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Unload vs End Statement

    Which is the best way (in order to comletely free memory) to close my program?
    To use the END statement (alone) or before this, to unload all of my forms?
    Does the END statement free the memory totally? (empties variable values, closes databases connections-recordsets, etc.)

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

  2. #2
    Join Date
    Aug 1999
    Location
    Pakistan
    Posts
    366

    Re: Unload vs End Statement

    Hello,
    The Unload statement frees up memory better than END.But to close databases you have to set the recordset property to "Nothing"
    Bye


  3. #3
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Unload vs End Statement

    I know that in order to close databases I must set the recordsets to nothing and I have already done it. But my question is that:
    I want to terminate the program execution immediatelly when an error occurs. So, what is the best way to do so? Just to use End st

  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Unload vs End Statement

    Hi

    Depending on the type's of components used in your project - the 'END' statement doesn't usually free up any memory ! It's a definite no-no as far as component based development goes and I haven't seen a single good argument in favour of using an 'END' statement anywhere in code.

    Why not simply unload all your forms and explicitly release your objects when you're finished with them ? It certainly makes debugging a whole lot easier.

    Chris Eastwood

    CodeGuru - the website for developers
    http://www.

  5. #5
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Unload vs End Statement

    Dear Chris,
    I did release my objects when I finished with them, but my question is what to do when an error occurs suddenly. I trap this error but what is the next step to do in order to close the program? To write all the fragment of code for closing all the databases AGAIN? Doesn't the END statement do it automatically?

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Unload vs End Statement

    This is straight from the MSDN :
    ---
    Note The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.
    ---

    I wouldn't trust them saying that all objects are cleared down / memory is freed - I know for a fact that if you use any subclassing or third party controls/dlls that an end-statement is like jumping out of a moving car to finish your journey - it may be ok but the chances are somethings going to crash sooner than later.

    The best way to quit your program would be to have a function that does all the relevant tidying up work - even if it's as simple as causing your main form to unload which then releases all your variables / api handles / whatever.

    eg.


    '
    ' In a code (BAS) module
    '
    Sub TidyUpAndQuit()
    Dim frm as Form

    for each frm in forms
    unload frm
    set frm = nothing
    next
    End Sub




    Make sure that all your Form_Unload procedures are clearing down any relevant object handles (set xx=nothing) and you shouldn't go far wrong.



    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  7. #7
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    I found it!

    So, here we are:
    There are two kinds of Memory: Heap and Stack.
    All the variables are stored to heap memory and are released with the End statement.
    All the other things (like the recordsets, connections to databases etc.) are stored to stack memory and are NOT released! So, we have to manually set all of them to nothing each time we want to immediatelly terminate our program.

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

  8. #8
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: I found it!

    To follow on - that is the reason that you should always do a

    set FormName = nothing



    after you have unloaded the form. Otherwise it'll just sit there holding on to any memory that may be in use by static variables etc.


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  9. #9
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Thanx!

    Thanx Chris for all!

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

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

Featured