Dispose or not??

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

    #1

    Dispose or not??

    I used to think that all objects that implement
    IDisposable should be disposed. But I found 80% of the
    classes implement IDisposable.

    But when I looked into the samples, most objects are not
    disposed. Generally, they're Gdi objects, form controls,
    most components, etc.

    So why? How can I decide to dispose or not?
  • Champika Nirosh

    #2
    Re: Dispose or not??

    Hi,

    In most cases you don't have to call dispose method explicitly. The garbage
    collector will dispose all unreachable object without puasing the interface.
    C# GC manages them well, so most of these sample, you have refered have let
    GC to mange it.

    Nirosh.

    "qrli" <anonymous@disc ussions.microso ft.com> wrote in message
    news:04f701c3b5 5c$25fd6cd0$a30 1280a@phx.gbl.. .[color=blue]
    > I used to think that all objects that implement
    > IDisposable should be disposed. But I found 80% of the
    > classes implement IDisposable.
    >
    > But when I looked into the samples, most objects are not
    > disposed. Generally, they're Gdi objects, form controls,
    > most components, etc.
    >
    > So why? How can I decide to dispose or not?[/color]


    Comment

    • Vijaye Raji

      #3
      Re: Dispose or not??

      Unless an object's class asks you to explicitly call Dispose, you don't have
      to. The GC will take care.

      Most GDI objects require you to call Dispose - to release their handle
      internally.

      vJ

      "qrli" <anonymous@disc ussions.microso ft.com> wrote in message
      news:04f701c3b5 5c$25fd6cd0$a30 1280a@phx.gbl.. .[color=blue]
      > I used to think that all objects that implement
      > IDisposable should be disposed. But I found 80% of the
      > classes implement IDisposable.
      >
      > But when I looked into the samples, most objects are not
      > disposed. Generally, they're Gdi objects, form controls,
      > most components, etc.
      >
      > So why? How can I decide to dispose or not?[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Dispose or not??

        qrli <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
        > I used to think that all objects that implement
        > IDisposable should be disposed. But I found 80% of the
        > classes implement IDisposable.[/color]

        80%? Where do you get that figure? That sounds awfully high to me.
        [color=blue]
        > But when I looked into the samples, most objects are not
        > disposed. Generally, they're Gdi objects, form controls,
        > most components, etc.[/color]

        Form controls will usually be disposed by the default code created by
        VS.NET.
        [color=blue]
        > So why? How can I decide to dispose or not?[/color]

        I would encourage you to always make sure you dispose of objects which
        implement IDisposable.

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

          #5
          Re: Dispose or not??

          Champika Nirosh <cnirosh@textce ntric.lk> wrote:[color=blue]
          > In most cases you don't have to call dispose method explicitly. The garbage
          > collector will dispose all unreachable object without puasing the interface.
          > C# GC manages them well, so most of these sample, you have refered have let
          > GC to mange it.[/color]

          That's a bad idea though, as most types which implement IDisposable
          either contain other IDisposables or directly contain native system
          resources. Leaving those to the garbage collector (which only knows
          about memory pressure, not Windows handle pressure etc) is a bad idea.

          --
          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: Dispose or not??

            Vijaye Raji <nospam_vganesh @hotmail.com> wrote:[color=blue]
            > Unless an object's class asks you to explicitly call Dispose, you don't have
            > to. The GC will take care.[/color]

            The GC will do it at *some* point, but the very fact that a type
            implements IDisposable means it's telling you that it would be
            preferable to call Dispose explicitly.

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

            • Guest's Avatar

              #7
              Re: Dispose or not??

              Most classes in the following namespaces implement
              IDisposable. And they're what I use most.
              System.Componen tModel
              System.Drawing
              System.Windows. Forms

              The code VS.NET generated doesn't dispose any thing
              except Timer and ImageList. Only Timer and ImageList,
              that is those have a constructor take an IContainer
              parameter, are added to the components field and are
              disposed in the Form.Dispose(bo ol).

              [color=blue]
              >-----Original Message-----
              >qrli <anonymous@disc ussions.microso ft.com> wrote:[color=green]
              >> I used to think that all objects that implement
              >> IDisposable should be disposed. But I found 80% of the
              >> classes implement IDisposable.[/color]
              >
              >80%? Where do you get that figure? That sounds awfully[/color]
              high to me.[color=blue]
              >[color=green]
              >> But when I looked into the samples, most objects are[/color][/color]
              not[color=blue][color=green]
              >> disposed. Generally, they're Gdi objects, form[/color][/color]
              controls,[color=blue][color=green]
              >> most components, etc.[/color]
              >
              >Form controls will usually be disposed by the default[/color]
              code created by[color=blue]
              >VS.NET.
              >[color=green]
              >> So why? How can I decide to dispose or not?[/color]
              >
              >I would encourage you to always make sure you dispose of[/color]
              objects which[color=blue]
              >implement IDisposable.
              >
              >--
              >Jon Skeet - <skeet@pobox.co m>
              >http://www.pobox.com/~skeet
              >If replying to the group, please do not mail me too
              >.
              >[/color]

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Dispose or not??

                <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                > Most classes in the following namespaces implement
                > IDisposable. And they're what I use most.
                > System.Componen tModel
                > System.Drawing
                > System.Windows. Forms[/color]

                They may be what you use most when doing GUI stuff, but that's very
                different from 80% of classes implementing IDisposable.
                [color=blue]
                > The code VS.NET generated doesn't dispose any thing
                > except Timer and ImageList. Only Timer and ImageList,
                > that is those have a constructor take an IContainer
                > parameter, are added to the components field and are
                > disposed in the Form.Dispose(bo ol).[/color]

                It *does* dispose things - just not where you can see them. To
                demonstrate this, create a simple form with just a label in it. Go into
                the code and change it from creating a label to creating a MyLabel, and
                define the MyLabel class as:

                class MyLabel : Label
                {
                protected override void Dispose(bool disposing)
                {
                MessageBox.Show ("Disposing: "+disposing );
                base.Dispose (disposing);
                }
                }

                Run it, and close the form - you'll see a message box saying
                "Disposing: true" which shows that Dispose has been called.

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

                • qrli

                  #9
                  Re: Dispose or not??

                  That just proves that all objects are disposed when the
                  program shut down.

                  I can give you another example. In the D3D9 Wizard
                  generated code, D3DSettingForm was created and there's no
                  Dispose call.

                  [color=blue]
                  >It *does* dispose things - just not where you can see[/color]
                  them. To[color=blue]
                  >demonstrate this, create a simple form with just a label[/color]
                  in it. Go into[color=blue]
                  >the code and change it from creating a label to creating[/color]
                  a MyLabel, and[color=blue]
                  >define the MyLabel class as:
                  >
                  >class MyLabel : Label
                  >{
                  > protected override void Dispose(bool disposing)
                  > {
                  > MessageBox.Show ("Disposing: "+disposing );
                  > base.Dispose (disposing);
                  > }
                  >}
                  >
                  >Run it, and close the form - you'll see a message box[/color]
                  saying[color=blue]
                  >"Disposing: true" which shows that Dispose has been[/color]
                  called.[color=blue]
                  >
                  >--
                  >Jon Skeet - <skeet@pobox.co m>
                  >http://www.pobox.com/~skeet
                  >If replying to the group, please do not mail me too
                  >.
                  >[/color]

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Dispose or not??

                    qrli <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                    > That just proves that all objects are disposed when the
                    > program shut down.[/color]

                    No - it's when the form itself is disposed (which it is automatically
                    on closing). To verify this, you could also do:

                    new Form1().Dispose (); // Or whatever your form is called

                    - and again, Dispose is called on all the controls added to it.
                    [color=blue]
                    > I can give you another example. In the D3D9 Wizard
                    > generated code, D3DSettingForm was created and there's no
                    > Dispose call.[/color]

                    Does D3DSettingsForm definitely not call Dispose internally when it's
                    closed though? I don't have the Managed DirectX SDK installed, but I
                    suspect it works the same way as the normal System.Windows. Forms does.

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

                    • qrli

                      #11
                      Re: Dispose or not??

                      see below
                      [color=blue][color=green]
                      >> That just proves that all objects are disposed when[/color][/color]
                      the[color=blue][color=green]
                      >> program shut down.[/color]
                      >
                      >No - it's when the form itself is disposed (which it is[/color]
                      automatically[color=blue]
                      >on closing). To verify this, you could also do:
                      >
                      >new Form1().Dispose (); // Or whatever your form is called
                      >
                      >- and again, Dispose is called on all the controls added[/color]
                      to it.

                      How to explain why ONLY Timer and ImageList are so
                      special that they must be added to components and
                      disposed explicitly if the form disposes all things
                      implicitly?

                      And again, why all samples in MSDN do not dispose GDI
                      objects?

                      I have a guess that rich resource objects can be left to
                      GC, e.g. GDI+ objects (32bit has not the former 16 bit
                      limit), Window Handles.
                      [color=blue]
                      >[color=green]
                      >> I can give you another example. In the D3D9 Wizard
                      >> generated code, D3DSettingForm was created and there's[/color][/color]
                      no[color=blue][color=green]
                      >> Dispose call.[/color]
                      >
                      >Does D3DSettingsForm definitely not call Dispose[/color]
                      internally when it's[color=blue]
                      >closed though? I don't have the Managed DirectX SDK[/color]
                      installed, but I[color=blue]
                      >suspect it works the same way as the normal[/color]
                      System.Windows. Forms does.

                      It's not disposed when closed, because I can access all
                      the properties and methods, even ShowDialog() again.

                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Dispose or not??

                        qrli <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
                        > How to explain why ONLY Timer and ImageList are so
                        > special that they must be added to components and
                        > disposed explicitly if the form disposes all things
                        > implicitly?[/color]

                        Presumably because they don't get added to the form in quite the same
                        way - they're not actually controls, so they won't go into a list of
                        controls that the form keeps around.
                        [color=blue]
                        > And again, why all samples in MSDN do not dispose GDI
                        > objects?[/color]

                        That's probably just bad samples. There are some real stinkers in
                        there, to be honest.
                        [color=blue]
                        > I have a guess that rich resource objects can be left to
                        > GC, e.g. GDI+ objects (32bit has not the former 16 bit
                        > limit), Window Handles.[/color]

                        I don't think that's a good idea, no.
                        [color=blue][color=green][color=darkred]
                        > >> I can give you another example. In the D3D9 Wizard
                        > >> generated code, D3DSettingForm was created and there's[/color][/color]
                        > no[color=green][color=darkred]
                        > >> Dispose call.[/color]
                        > >
                        > >Does D3DSettingsForm definitely not call Dispose[/color]
                        > internally when it's[color=green]
                        > >closed though? I don't have the Managed DirectX SDK[/color]
                        > installed, but I[color=green]
                        > >suspect it works the same way as the normal[/color]
                        > System.Windows. Forms does.
                        >
                        > It's not disposed when closed, because I can access all
                        > the properties and methods, even ShowDialog() again.[/color]

                        That doesn't *necessarily* mean it's not disposed, although it suggests
                        it. Could well just be that the DX9 stuff isn't written quite as well
                        as the Windows.Forms stuff. That isn't a good excuse for not calling
                        Dispose in your own code.

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

                        • Vijaye Raji

                          #13
                          Re: Dispose or not??

                          Hmm.. so, we really have to call Dispose on pretty much all user-created
                          objects (since most of them derive from IDisposable). That's good to
                          know...

                          Just curious - why couldn't they have made GC call Dispose when it cleans up
                          the object (if it implements IDisposable)?

                          Thanks,
                          vJ

                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          news:MPG.1a30f0 17516306db989b5 c@msnews.micros oft.com...[color=blue]
                          > Vijaye Raji <nospam_vganesh @hotmail.com> wrote:[color=green]
                          > > Unless an object's class asks you to explicitly call Dispose, you don't[/color][/color]
                          have[color=blue][color=green]
                          > > to. The GC will take care.[/color]
                          >
                          > The GC will do it at *some* point, but the very fact that a type
                          > implements IDisposable means it's telling you that it would be
                          > preferable to call Dispose explicitly.
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Dispose or not??

                            Vijaye Raji <nospam_vganesh @hotmail.com> wrote:[color=blue]
                            > Hmm.. so, we really have to call Dispose on pretty much all user-created
                            > objects (since most of them derive from IDisposable). That's good to
                            > know...[/color]

                            No, you don't - because by the sounds of it, most objects which you're
                            creating are being placed on forms, and as I've said before, they're
                            disposed automatically when the form itself is disposed.

                            I rarely have to call Dispose (in fact I very very rarely call it
                            directly - I usually just have a using(...) block) apart from when I'm
                            using streams/readers/database objects.
                            [color=blue]
                            > Just curious - why couldn't they have made GC call Dispose when it cleans up
                            > the object (if it implements IDisposable)?[/color]

                            Well, it calls the finalizer, which will usually call Dispose itself.
                            The point of calling Dispose explicitly is to make it happen in a
                            timely manner - there's no guarantee about when the GC will get round
                            to cleaning up.

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

                            • Niall

                              #15
                              Re: Dispose or not??

                              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              news:MPG.1a3123 dcf962f68d989b5 f@msnews.micros oft.com...[color=blue]
                              > qrli <anonymous@disc ussions.microso ft.com> wrote:[color=green]
                              > > How to explain why ONLY Timer and ImageList are so
                              > > special that they must be added to components and
                              > > disposed explicitly if the form disposes all things
                              > > implicitly?[/color]
                              >
                              > Presumably because they don't get added to the form in quite the same
                              > way - they're not actually controls, so they won't go into a list of
                              > controls that the form keeps around.[/color]

                              This is correct. In Control.Dispose (bool), the .Net Control class calls
                              Dispose on all controls in the Control.Control s collection. This only occurs
                              within the branch of Dispose taken when it is not called from a Finalizer.
                              As Jon says, the Timer and ImageList are not controls, so they cannot be
                              places in the Control.Control s collection, and hence the base Control class
                              cannot take care of disposing them for you. This is why the template used by
                              VS for a Windows Form generates a components collection and a dispose for
                              the Form that will ensure the components collection is disposed as well.

                              [color=blue][color=green]
                              > > I have a guess that rich resource objects can be left to
                              > > GC, e.g. GDI+ objects (32bit has not the former 16 bit
                              > > limit), Window Handles.[/color]
                              >
                              > I don't think that's a good idea, no.[/color]

                              It's a performance hit for starters. If you leave the controls to the GC,
                              you guarantee that you will have to wait longer for the resources to be
                              free, as well as for the memory of the objects themselves to be freed. This
                              is because it takes two rounds of the GC to fully collect a finalizable
                              object. Component.Dispo se uses GC.SuppressFina lize, so disposing a Control
                              ensures that it will be collected in one GC, not two.

                              This means a non-disposed control is very likely to end up in Gen 2, which
                              is only rarely collected. So a non-disposed control can take significantly
                              longer to be collected than a disposed one, and hence any objects directly
                              or indirectly referenced by the control will take longer as well.

                              Niall


                              Comment

                              Working...