Re: Scoping in VB.Net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Troy (poster_at_designguide.com)
Date: 02/13/04


Date: Fri, 13 Feb 2004 11:31:34 -0800


=?Utf-8?B?Sm9obiBL?=,

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

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.

-- 
--
Regards,
Troy


Relevant Pages

  • Why does a WinForm stay in memory if the local reference var goes out of scope?
    ... Why does a WinForm stay in memory if the local reference var goes out ... of scope? ... FormTest is a form with nothing on it, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Garbage Collection Eligibility and portability
    ... that alters exactly when a variable reference to a memory ... scope where the variable is available the referenced memory is ... collection if a reference is found in any of those areas. ... Now the garbage collector can at least blow away all but the first ...
    (comp.lang.lisp)
  • Re: Resolve object pointer any use for this?
    ... would strongly advise not to play with weak references unless you absolutely ... It wouldn't save you any memory, ... could instead pass the ObjPtr and restore to the full reference in the dll. ... out of scope in the mean time, then you have passed an invalid memory address ...
    (microsoft.public.excel.programming)
  • Re: Garbage Collection - dim x as y versus dim x as new y
    ... Well if the object goes out of scope it will automaticly be released ... although i am still one of those people who like to set object pointers to ... > current understanding is that (to avoid memory leaks, ... > isn't so much HOW an object reference ) ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why does a WinForm stay in memory if the local reference var goes out of scope?
    ... "Ernst Raedecker" schrieb: ... of scope? ... I assume the form doesn't get GCed because the Windows Forms library internally still holds a reference to the form. ...
    (microsoft.public.dotnet.framework.windowsforms)