kjack
kjack

Reputation: 2014

What unloads the modules when a vb6 program terminates

The proper way to end a vb6 program is to do something like this :

Dim frm As Form
   For Each frm In Forms 
       Unload frm
   Next frm
   end

That takes care of forms, what takes care of modules in memory?

Upvotes: 2

Views: 4360

Answers (5)

JMMDL
JMMDL

Reputation: 11

The method of Unloading ALL MODULES on exit had resolved a funny bug for retrieving an exit code with API call ExitProcess()

Upvotes: 0

Bob
Bob

Reputation: 569

If by "module" you mean static .BAS modules (they're all modules: Forms, Classes, UserControls, etc.) you don't need to "unload" them because they're static.

Most other module types are dynamically loaded. In the case of Forms typically via the predeclared global reference variable with the same name and type as the Form class (yes, a Form is a kind of class).

Upvotes: 2

RS Conley
RS Conley

Reputation: 7192

Note that if you use global variables then you should check if any of them need to get cleaned up. If so you should have a CleanUp method of your module and call that during the unload event of your main form. Another gotcha is sometimes the ORDER of how you unload things is important. By doing an explicit cleanup you can control that.

In VB6 you should rarely use END. If you have any circular references the program will remain as a process chewing up resource causing various strange bugs when you fire it up again. With COM objects is very easy to in inadvertently setup a chain of object thats is circularly linked.

End exists as a compatibility holdover from previous version of QuickBASIC and Visual BASIC. It did not start causing major problems until VB 4.X introduced the ability to create classes. It started gaining attention in 5.X.

Prior to this one of the only ways to make this happen in VB 3.X is to have two forms set references to each other.

Upvotes: 1

JeffK
JeffK

Reputation: 3039

There is no need to explicitly unload modules in VB6. They are unloaded automatically when the last form is unloaded. The language doesn't support references to standard modules at all, only to the (global) functions and variables defined therein. Since you can't reference the module, you can't unload them either.

Upvotes: 4

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Don't use End, you don't need to (ever). Then all memory gets freed properly.

Even using End, memory should be freed automatically. There was a rumor that some class instances are forgotten and don't get terminated correctly, despite reference counting. Thus, it's been established as best practice to set all object instances to Nothing explicitly (especially, but not limited to instances allocated in modules). I've never seen any confirmation that this is actually true (it might still be, though!).

Upvotes: 3

Related Questions