Do I need to implement a Dispose method?

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

    #1

    Do I need to implement a Dispose method?

    I have a class which when instatiated creates a new XmlDocument object and
    loads an xml file into it. It is used as follows:

    MyObject obj = new MyObject("books .xml");

    If I later have

    obj = new MyObject("books 2.xml");

    Would all memory referenced by 'obj' (i.e. the XmlDocument) from the first
    call be freed or would I be required to implement a Dispose method to
    release the memory associated like:

    public void Dispose()
    {
    m_Xml = null;
    }

    From what I can make out, as XmlDocument is a managed resource (with no
    explicit Dispose() or Close() method) it will automatically be deallocated.

    Wayne.


  • Jon Skeet [C# MVP]

    #2
    Re: Do I need to implement a Dispose method?

    wh <wayne@nospam.p yesmeadow.com> wrote:[color=blue]
    > I have a class which when instatiated creates a new XmlDocument object and
    > loads an xml file into it. It is used as follows:
    >
    > MyObject obj = new MyObject("books .xml");
    >
    > If I later have
    >
    > obj = new MyObject("books 2.xml");
    >
    > Would all memory referenced by 'obj' (i.e. the XmlDocument) from the first
    > call be freed or would I be required to implement a Dispose method to
    > release the memory associated like:
    >
    > public void Dispose()
    > {
    > m_Xml = null;
    > }
    >
    > From what I can make out, as XmlDocument is a managed resource (with no
    > explicit Dispose() or Close() method) it will automatically be
    > deallocated.[/color]

    Setting

    obj = new MyObject("books 2.xml"); doesn't free up memory immediately,
    but then neither does m_Xml = null;

    You just don't free up memory immediately in .NET - the garbage
    collector takes care of it. If you've got *unmanaged* resources within
    your object, or references to any objects which themselves implement
    IDisposable, you should dispose of those appropriately. However, that
    should be a fairly rare occurrence, IMO.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • wh

      #3
      Re: Do I need to implement a Dispose method?

      Thank you for your reply.

      So can I assume that *if* XmLDocument did implement a Dispose() method then
      I should provide a Dispose() method in my class and call the Dispose() of
      the XmlDocument?

      Wayne.


      Comment

      • wh

        #4
        Re: Do I need to implement a Dispose method?

        Thank you for your reply.

        So can I assume that *if* XmLDocument did implement a Dispose() method then
        I should provide a Dispose() method in my class and call the Dispose() of
        the XmlDocument?

        Wayne.


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Do I need to implement a Dispose method?

          wh <wayne@nospam.p yesmeadow.com> wrote:[color=blue]
          > So can I assume that *if* XmLDocument did implement a Dispose() method then
          > I should provide a Dispose() method in my class and call the Dispose() of
          > the XmlDocument?[/color]

          Yes.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Do I need to implement a Dispose method?

            wh <wayne@nospam.p yesmeadow.com> wrote:[color=blue]
            > So can I assume that *if* XmLDocument did implement a Dispose() method then
            > I should provide a Dispose() method in my class and call the Dispose() of
            > the XmlDocument?[/color]

            Yes.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...