Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Garbage Collection

Status
Not open for further replies.

NatHunter

Technical User
Aug 17, 2001
51
0
0
GB
I am new to .NET and have a problem with VB.NET garbage collection.

I have declared a form in a module as public, and call it from the main program form. However, once I close it (.close), it is 'garbaged collected' and seems to lose its declaration, and therefore my app errors when I try to show the form again. I have resorted to hiding the form, but is there a neater way of handling this?

I've declaring it locally, but

modGlobals:
============

Public frmBankSettings As New frmbank()

MainForm:
=========
Public Sub mnuBanker_Click(...) Handles mnuBanker.Click
'Dim frmbanksettings As New frmbank()
frmBankSettings.Show()
End Sub
 
You're doing it correctly -- what you have is not a problem with the garbage collector, but simply with a variable that's going out of scope.

If you want the form to stick around, declare it's variable at a higher level of scope, or pass it back up the call chain via a parameter.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
The problem is that a form can not be reused afer it is closed.

modGlobals:
============

Public frmBankSettings As frmbank()

MainForm:
=========

Public Sub mnuBanker_Click(...) Handles mnuBanker.Click
if frmbanksettings = Nothing then
frmBankSettings = New frmbank()
End If

frmBankSettings.Show()
End Sub
.....
frmBankSettings.Close()
frmBankSetting = Nothing

Compare Code
 
Here's a question John, if a form is set to nothing when it is closed, which is what I've assuming you're checking for in the first block. Then why would you need to set it = Nothing again after closing it? Wouldn't the .Close take care of that?

-Rick

----------------------
 
No. The object, nstance of a class, does not know anything about the variables containing rwferences to it.
frmBankSettings is a variable in your code that contains a reference to an instance of the form. Think about it.
If I have an object called A with a Close method, how could I set the caller's variable to Nothing in the Close method?

Public Class A
Public Sub Close()
......write code here to set caller's instane variable to Nothing
End Sub

'''''Caller
Dim objA as new A
objA.Close
' objA still references an instance of A




Compare Code
 
If I have an object called A with a Close method, how could I set the caller's variable to Nothing in the Close method?"

That's what I wanted to make sure of. But then, if .close does not kill the variable, why can we not .open a form that has been .closed?

-Rick

----------------------
 
Short answer. Because that is the way the Form class was written.

Remember, the Close method is provided so that critical machine resources can be freed immediately rather than waiting some time for Garbage Collection. Just what resources those are in form, I do not know. I do know that it is usually easier with a complicated Class like a Bank Account to require a New object for each account with auto-initialization rather than provide all the code needed to completely re-initiaize an existing object for reuse. In many cases, initialization done by New processng is much faster than "own code" re-initialization and is not subject to the programmer's "forgetting" to initialize a variable.

Compare Code
 
John -
The Form object contains a Windows Handle, which is an unmanaged Win32 object. So a .close method (or equivalent) is needed to ensure it gets freed in a timely manner, as there is a limit on the number of handles that a process may own. A large limit, to be sure, but it still exists.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Any control that will be painting to the form will have at least one window handle. Often, parts of the control will have their own window handle. Grid controls are sometimes written this way -- each cell has it's own window handle.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top