unsure about object disposal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    #1

    unsure about object disposal


    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


  • Peter Bromberg [C# MVP]

    #2
    RE: unsure about object disposal

    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
    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "Steve" wrote:
    >
    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
    >
    >
    >

    Comment

    • Bruce Wood

      #3
      Re: unsure about object disposal


      Steve wrote:
      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.

      Comment

      Working...