Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. IDisposable

IDisposable

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    blackthunder001
    wrote on last edited by
    #1

    According to MSDN doc, when I derive a class that implements IDisposable, I need to override protected virtual void Dispose(bool disposing). And in this function, if disposing is true, I need to call Dispose() on all managed objects that I own (in addition to releasing unmanaged resources). Now, when I create a new Form object, the wizard gives me the overridden Dispose(bool) method with the only statement being: if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); However, if I add a control, say a button named button1, it doesn't add the statement button1.Dispose() into the above method. Is this a mistake? I mean, do I need to manually add this statement, or is it already called implicitly somewhere?

    H 1 Reply Last reply
    0
    • B blackthunder001

      According to MSDN doc, when I derive a class that implements IDisposable, I need to override protected virtual void Dispose(bool disposing). And in this function, if disposing is true, I need to call Dispose() on all managed objects that I own (in addition to releasing unmanaged resources). Now, when I create a new Form object, the wizard gives me the overridden Dispose(bool) method with the only statement being: if (disposing) { if (components != null) components.Dispose(); } base.Dispose(disposing); However, if I add a control, say a button named button1, it doesn't add the statement button1.Dispose() into the above method. Is this a mistake? I mean, do I need to manually add this statement, or is it already called implicitly somewhere?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First, the Dispose(bool) method doesn't NOT need to be overridden and is not declared in IDisposable. IDisposable declares one method: void Dispose(). Dispose(bool) is a virtual method inheritted from Control. You can implement IDisposable on anything. You don't need to override Dispose(bool) unless you contain components that need to be disposed, like AxHost controls (ActiveX controls in a managed environment) and stuff like that. Managed objects are managed by the garbage collector. They will be garbage collected when necessary. You typically implement IDisposable (or override Dispose(bool) in your controls) when you need to clean-up unmanaged resources like window handles, file handles, and the like. These things are unmanaged, i.e. they are not managed by the garbage collector (GC). Objects that don't need to free unmanaged resources can implement and use IDisposable to free managed resources, but should use GC.SurpressFinalize(this) in the Dispose() implementation to prevent the GC from wasting time cleaning it up (since you already did). Other reasons for implementing IDisposable and for callers to call Dispose is to release resources immediately since the GC doesn't run continuously. This makes sure that resources (even managed resources) are released when you want them to be. For things like a Button, it typically doesn't matter. The GC will get around to it. Dispose it only if it's absolutely necessary (otherwise you degrade the performance of your application since the GC is taking time away from your program's execution). See Programming for Garbage Collection[^] in the .NET Framework SDK for more information and examples.

      Microsoft MVP, Visual C# My Articles

      B 1 Reply Last reply
      0
      • H Heath Stewart

        First, the Dispose(bool) method doesn't NOT need to be overridden and is not declared in IDisposable. IDisposable declares one method: void Dispose(). Dispose(bool) is a virtual method inheritted from Control. You can implement IDisposable on anything. You don't need to override Dispose(bool) unless you contain components that need to be disposed, like AxHost controls (ActiveX controls in a managed environment) and stuff like that. Managed objects are managed by the garbage collector. They will be garbage collected when necessary. You typically implement IDisposable (or override Dispose(bool) in your controls) when you need to clean-up unmanaged resources like window handles, file handles, and the like. These things are unmanaged, i.e. they are not managed by the garbage collector (GC). Objects that don't need to free unmanaged resources can implement and use IDisposable to free managed resources, but should use GC.SurpressFinalize(this) in the Dispose() implementation to prevent the GC from wasting time cleaning it up (since you already did). Other reasons for implementing IDisposable and for callers to call Dispose is to release resources immediately since the GC doesn't run continuously. This makes sure that resources (even managed resources) are released when you want them to be. For things like a Button, it typically doesn't matter. The GC will get around to it. Dispose it only if it's absolutely necessary (otherwise you degrade the performance of your application since the GC is taking time away from your program's execution). See Programming for Garbage Collection[^] in the .NET Framework SDK for more information and examples.

        Microsoft MVP, Visual C# My Articles

        B Offline
        B Offline
        blackthunder001
        wrote on last edited by
        #3

        Actually I wrote the email after reading the Proramming for Garbage Collection article. I guess the reason why I need to Dispose() managed objects like Button() is if holds unmanaged resources, such as maybe icon, brush, etc. But now I realise that I probably don't need to call Dispose() directly on these controls, because when I call base.Dispose(bool) in the Dispose(bool) override that I have in my form, it will presumably call Dispose() for every control that it has (in the Controls collection). Other components such as Timer will be disposed when I call components.Dispose(). This is a guess, which I will verify later on.

        H 1 Reply Last reply
        0
        • B blackthunder001

          Actually I wrote the email after reading the Proramming for Garbage Collection article. I guess the reason why I need to Dispose() managed objects like Button() is if holds unmanaged resources, such as maybe icon, brush, etc. But now I realise that I probably don't need to call Dispose() directly on these controls, because when I call base.Dispose(bool) in the Dispose(bool) override that I have in my form, it will presumably call Dispose() for every control that it has (in the Controls collection). Other components such as Timer will be disposed when I call components.Dispose(). This is a guess, which I will verify later on.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          You're really worrying about it too much. The controls and many of the other classes in the FCL will clean themselves up just fine in due time when the GC comes around to collect them. Unless you're strapped for memory (which could be signs of a bad design as well), don't worry about it. If a class should be explicitly disposed, it will typically tell you, like the Graphics class if you create it yourself (never dispose of it from the Paint event or the OnPaint override - the callee will do that when it's ready).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups