Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Topaz on July 11, 2005, 11:50 AM

Title: [VB6] Leaving itself in Memory
Post by: Topaz on July 11, 2005, 11:50 AM
This application that I'm developing is always leaving itself in memory after I close it. I unloaded all the forms and ended it, but it seems to do it anyway. Ideas?
Title: Re: [VB6] Leaving itself in Memory
Post by: Dyndrilliac on July 11, 2005, 12:43 PM
Make a call to ExitProcess().
Title: Re: [VB6] Leaving itself in Memory
Post by: Adron on July 11, 2005, 06:10 PM
You could use End to get the same effect, but VB programs are supposed to end by themselves when you unload all forms and return from your Sub Main if you have one. Maybe you're forgetting some form?
Title: Re: [VB6] Leaving itself in Memory
Post by: Topaz on July 13, 2005, 11:51 AM
Then I should Unload all of them then End? I'm sure I unloaded all of them.
Title: Re: [VB6] Leaving itself in Memory
Post by: warz on July 13, 2005, 09:01 PM
Should be able to simply End your main form.
Title: Re: [VB6] Leaving itself in Memory
Post by: Grok on July 19, 2005, 10:43 AM
Quote from: Topaz on July 13, 2005, 11:51 AM
Then I should Unload all of them then End? I'm sure I unloaded all of them.

How sure are you?

In your main form's Unload, try this:

Private Sub Form_Unload(Cancel As Integer)
    Dim F As Form
    For Each F In Forms
        If StrComp(F.Name, Me.Name) <> 0 Then
            Unload F
        End If
        Set F = Nothing
    Next
End Sub


If this succeeds in solving our problem, you had a hidden form that did not close like you thought.
If your application is still open after this, you have loaded code running in an object that is held open in a module-scoped variable.  Go through your Module1.BAS (or other BAS modules) for any Objects/Variants that might be maintaining an instance of a class.  Trace that class' execution by writing debug logs on Initialize and Terminate events.  If you have an Init without a Term, there's the problem.
Title: Re: [VB6] Leaving itself in Memory
Post by: MyndFyre on July 19, 2005, 01:21 PM
OT:

Quote from: Grok on July 19, 2005, 10:43 AM
How sure are you?

*blink*

GROK!
Title: Re: [VB6] Leaving itself in Memory
Post by: Chriso on August 16, 2005, 06:32 PM
I've had this problem before, I think it was a procedure that was running on Form_Load that told the form to show, the procedure was to downloading a file via Async. I had to make sure it stopped everything before it was unloaded otherwise the form would show itself again.
Title: Re: [VB6] Leaving itself in Memory
Post by: Elneroth on October 13, 2005, 06:36 PM
A lot of people get that problem a lot.
Just add the line End to all the form_unloads you want to end the program when you close them.

Public Sub Form_Unload()
End
End Sub
Title: Re: [VB6] Leaving itself in Memory
Post by: Grok on December 08, 2005, 02:50 PM
Quote from: Elneroth on October 13, 2005, 06:36 PM
A lot of people get that problem a lot.
Just add the line End to all the form_unloads you want to end the program when you close them.

Public Sub Form_Unload()
End
End Sub


Please do not suggest this again, it promotes lazy programming by making the programmer not responsible for their code behavior.  Using End hides the fact that objects are being created and code invoked yet no proper exit point or deallocation of objects has taken place.
Title: Re: [VB6] Leaving itself in Memory
Post by: rabbit on December 08, 2005, 07:32 PM
You should really For Each through your forms calling Unload on each.
Title: Re: [VB6] Leaving itself in Memory
Post by: FrOzeN on December 08, 2005, 11:16 PM
I'd just like to mention regarding Grok's code.

With StealthBot, a bug had existed for a long time that when you closed StealthBot by clicking 'Exit' in the right-click popup menu in the tray, it would crash giving an error.
StealthBot's code is setup the exact same way as Grok had to Unload each form and Set them as Nothing. And to then call 'End' after, but the problem still existed.

Stealth fixed this by changing 'End' to use ExitProcess(). (As Dyndrilliac said).

Due to this I'd recommend using ExitProcess() to assure avoidance of even the most unorthodox errors that may occur. :)
Title: Re: [VB6] Leaving itself in Memory
Post by: Adron on December 09, 2005, 12:06 AM
What about a form containing this code?


Sub Command1_Click()
   Static running As Boolean
   If running Then running = false: Exit Sub
   running = true
   Do While running
      Sleep 100
      DoEvents
   Loop
End Sub


I have a feeling that will not unload willingly while the loop is running?