Click to See Complete Forum and Search --> : Unload vs End Statement


Dr_Michael
August 2nd, 1999, 08:18 AM
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

chem1
August 2nd, 1999, 08:34 AM
Hello,
The Unload statement frees up memory better than END.But to close databases you have to set the recordset property to "Nothing"
Bye

Dr_Michael
August 2nd, 1999, 08:49 AM
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

Chris Eastwood
August 2nd, 1999, 08:51 AM
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.

Dr_Michael
August 2nd, 1999, 09:17 AM
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

Chris Eastwood
August 2nd, 1999, 09:32 AM
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

Dr_Michael
August 2nd, 1999, 10:07 AM
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

Chris Eastwood
August 2nd, 1999, 10:18 AM
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

Dr_Michael
August 2nd, 1999, 11:00 AM
Thanx Chris for all!

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