Disposing object is a collection

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

    #1

    Disposing object is a collection

    Question:

    If I have a class which has a property which is a collection of widget
    objects (an arrayList of widgets), and both the containter class and the
    widget class implement the IDisposable interface, does the container class
    need to call the dispose method of each of the widgets when its dispose
    method is called, or does this happen automatically because the container
    class will go "out of scope"?

    Thanks for any help!

    -D


  • Fergus Cooney

    #2
    Re: Disposing object is a collection

    Hi MC D,

    If the Collection is the only owner of these objects, I'd say Dispose of them explicitly. But if there may be other
    references floating around, you would be applying a knee to their balls! Ouch.

    It not done, it will happen automatically when the GC can be bothered, so to speak.

    Regards,
    Fergus


    Comment

    • MC D

      #3
      Re: Disposing object is a collection

      I guess part of my question is, if each of these is saying they implement
      IDisposable, then is the framework "smart" enough to know that any reference
      in an object to another object that implements Idisposeable is automatically
      disposed of when its parent calls its own dispose method. Make sense?

      Fergus makes a good point about external references to items in the
      collection... I dunno... how is this usually handled? There is nothing
      keeping someone for making a reference to an item in the objects collection,
      then disposing the "parent" object... but that would seem pretty stupid... I
      mean, If I'm going to dispose the object that gave me the reference in the
      first place, I'd expect any other references to that objects members to go
      bye-bye too!

      -D

      "Fergus Cooney" <filter@post.co m> wrote in message
      news:upK2RWIpDH A.2652@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Hi MC D,
      >
      > If the Collection is the only owner of these objects, I'd say Dispose[/color]
      of them explicitly. But if there may be other[color=blue]
      > references floating around, you would be applying a knee to their balls![/color]
      Ouch.[color=blue]
      >
      > It not done, it will happen automatically when the GC can be bothered,[/color]
      so to speak.[color=blue]
      >
      > Regards,
      > Fergus
      >
      >[/color]


      Comment

      • Fergus Cooney

        #4
        Re: Disposing object is a collection

        Hi MC D,

        || I guess part of my question is, if each of these is saying they
        || implement IDisposable, then is the framework "smart" enough
        || to know that any reference in an object to another object that
        || implements Idisposeable is automatically disposed of when its
        || parent calls its own dispose method. Make sense?

        Yep, makes sense and no, for the reason hinted at before. Within the Framework, only the GC knows who and how many,
        when it comes to references. And at that point the question's past its sell-by date. If a class knows that it <should> be
        the only one with references to its Widgets then it should Dispose when possible - if there are other refs in such a case,
        let them get their Exception, it's deserved. But if others <may> have refs, leave well alone.

        || There is nothing keeping someone for making a reference to an
        || item in the objects collection, then disposing the "parent" object...
        || but that would seem pretty stupid

        I can imagine wanting to keep a few TreeNodes but getting rid of the Tree, for instance. I think it's like Clone - is
        it deep or is it shallow? Well, it's up to the designer - they'll have 'guessed/decided' which seems most reasonable.
        Likewise Dispose - shallow or deep? The answer has to be, "well, it depends".... I would expect that more often than not,
        Dispose is shallow. It would be up to the user of the container to iterate and Dispose of the contents and then finally
        Dispose of the container, too.

        Regards,
        Fergus


        Comment

        • Alan Pretre

          #5
          Re: Disposing object is a collection

          "MC D" <aspam@earthtal k.com> wrote in message
          news:%236enfKIp DHA.1740@TK2MSF TNGP12.phx.gbl. ..[color=blue]
          > If I have a class which has a property which is a collection of widget
          > objects (an arrayList of widgets), and both the containter class and the
          > widget class implement the IDisposable interface, does the container class
          > need to call the dispose method of each of the widgets when its dispose
          > method is called, or does this happen automatically because the container
          > class will go "out of scope"?[/color]

          ArrayList in and of itself does not implement IDisposable. You could derive
          your own class from ArrayList and implement IDisposable there, then build
          the intelligence into it to dispose its members when the container gets
          disposed.

          -- Alan


          Comment

          Working...