Link to home
Start Free TrialLog in
Avatar of Norma Posy
Norma PosyFlag for United States of America

asked on

Unloading a form

Using VB6

Does that little “X” in the upper R.H. corner of a form perform an “Unload”?

Reason why I ask: I’ve known poorly written code to leave behind a VB form loaded in RAM after exiting. I believe this is known as a “memory leak”.

My application has a main form, from which various secondary forms are invoked. Each of those has a “Return” command button with the code:

Sub cmdReturn_Click()
Unload Me
End Sub

So, if the user instead clicks on the little “X”, does the same thing happen?


Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

>Does that little “X” in the upper R.H. corner of a form perform an “Unload”?

No. It hides the form and generates a QueryClose event.

>I’ve known poorly written code to leave behind a VB form loaded in RAM after exiting. I believe this is known as a “memory leak”.

This is not a memory leak in the classic sense. The form exists in memory and can be re-shown at any time. A memory leak is chucks of memory that are not disposed but are also no longer accessible because the handle has been lost.

>So, if the user instead clicks on the little “X”, does the same thing happen?

No. You have to unload the form yourself using code.

Kevin
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I was wrong. Erick37 is correct. When the close box is clicked the form WILL be unloaded once the Terminate event is fired and any code in that event handler run.

Should you decide that you do not want the form unloaded you can force it to remain in memory by canceling the QueryUnload and executing the Hide method instead.

You can also hide the close box but that requires a Windows API call.

Kevin
Avatar of Norma Posy

ASKER

Erick: The link was very useful.
Thank you.

Kevin: I appreciated your discussion.
Thank you.