Scoping in VB.Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clic
Dim aform As Form
aform = New Form
aform.Show(
End Su

since aform is a local variable, when the function ends, aform goes out of scope (or I thought it should
There are no other references to the instance of Form2, therefore I would have expected the Form2 to close be garbage collected)... but it stayed visible.... why?
 
Hi John,

The variable did go out of scope. You can no longer reference the window
using aform. There is at least one other reference to the window however...
it's on display right? That means it is receiving events which means that
Windows knows about it.

Tom

John K. said:
I have the following:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim aform As Form2
aform = New Form2
aform.Show()
End Sub

since aform is a local variable, when the function ends, aform goes out of scope (or I thought it should)
There are no other references to the instance of Form2, therefore I would
have expected the Form2 to close be garbage collected)... but it stayed
visible.... why?
 
Well John I looked all over the web for an explanation but I didn't find
one. Somebody here probably knows for certain but I can almost guarantee
that there is a live reference to the form so long as it is displayed. If
you you close the form that reference will disappear.

Sorry I couldn't point to something definitive... I looked all over.

Tom

John K said:
yes it is still visible & it seems to be still accepting events. So why
if no one has a reference to it within the application is it still hanging
around & visible? Would have thought "Windows" itself does not count as a
reference...
 
=?Utf-8?B?Sm9obiBL?=,

You may be slightly confused about scope, objects and object lifetime.
Scope is not equivalent to object lifetime. Objects generallly are not
subject to scope (generally).

As an example of what I mean, think about memory leaks using a language
like C++. If you create a new object from a class Form2 in a
function/method in C++ and do not call the delete operator on that
object before the function/method is finished the memory
created/allocated (on the heap-not the stack) for that object remains
allocated through in OS even though YOU the programmer cannot ever
access that object again or it's memory location (it went out of scope).
This means that the memory is lost to you and other programs until you
reboot your machine since there is no way for YOU to get a handle on
that area in memory. The OS considers it in use. This is not the case
for value type variables (non-class or object variables)

Object variables are pointers and/or references to an area in memory.
This is different than value type variables like int's, char's and etc.
Those are value type variables in which case they are declared on the
stack and as soon as your function leaves - so do they. Object
variables are not value types (but reference types) and are declared on
the heap and therefore are not subject to the scope declared on the
stack...

That assumes that you have working knowledge of C++ and/or similar
object oriented languages. On the other hand - .NET protects against
memory leaks via the garbage collector. .NET framwork is concious about
every object you create (even Form2). It will check to see if there are
any references still pointing to the object in memory (Form2) - if not,
it will gargabe collect (at random or when necessary), if there are
references will not do anything. In your case there is still a
reference to it. As long as you can see it on your screen there is a
reference to it (or if you hide the window, there will still be a
reference to it). The Windows OS has a reference to it via a "Handle".
Windows manages all windows. <g>

Now when you declare Form2 it's scope is only within your method Button1
_Click()... meaning YOU the programmer can only access it within that
method. But scope doesn't always mean that the object dies.

In any case... (I've rambled on too much)... The short answer is:

Yes, somebody DOES have a reference to it and that somebody is the
Windows OS. When you close that window... it is out of scope (as you
said before) AND it will be released memory via garbage collection (in
..net) as no more references will be attached to it.

--
 
Back
Top