unsure about object disposal

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I instantiate a form and within that form I instatiate an object from a
class i created.
When i close the form it is no longer referenced anywhere so my
understanding is that the
garbage collector will take care of it and no memory leakage will occur.

I presume in this scenario that the object that was created in the form
is also now not referenced
anywhere and will also be taken care of by the garbage collector.

This is all presumption on my part as i have been unable to find
documentation.

Can anyone confirm that my premise is correct?

rgds,Steve
 
Yes, yes, and yes.
Unless objects hold references to unmanaged resources and need to be
disposed, eventually the GC infrastructure will take care of it. So unless
you are dealing with sockets, connections, file handes, graphics objects,
etc. then "dont worry, be happy".
Peter
 
Steve said:
I instantiate a form and within that form I instatiate an object from a
class i created.
When i close the form it is no longer referenced anywhere so my
understanding is that the
garbage collector will take care of it and no memory leakage will occur.

I presume in this scenario that the object that was created in the form
is also now not referenced
anywhere and will also be taken care of by the garbage collector.

This is all presumption on my part as i have been unable to find
documentation.

Can anyone confirm that my premise is correct?

There's only one case in which you have to worry, and that is if your
Form subscribes to any events on objects that are still "alive"
(referenced by something that eventually leads back to the stack) or
static events (which in effect are always referenced).

An event subscription counts as a reference from the object that raises
the event to the subscriber, so if you (explicitly) had your Form
subscribe to a static event (for example) then there will always be a
reference and it will never be collected.

However, in the particular scenario you outlined, yes, the object
referenced by the now-dead form will be collected along with the form.
 
Back
Top