=?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.
--