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
  1. Home
  2. General Programming
  3. C#
  4. Dispose pattern [modified]

Dispose pattern [modified]

Scheduled Pinned Locked Moved C#
csharpregexperformancetutorialquestion
4 Posts 3 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.
  • C Offline
    C Offline
    cateyes99
    wrote on last edited by
    #1

    When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:

    protected virtual void Dispose(bool disposing)
    {
    if (!this.disposed)
    {
    if (disposing)
    {
    // Dispose managed resources.
    }
    // Dispose unmanaged resources.
    }
    disposed = true;
    }

    When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:

    public void Dispose()
    {
    Dispose(true);
    GC.SuppressFinalize(this);
    }

    the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007

    C S 2 Replies Last reply
    0
    • C cateyes99

      When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:

      protected virtual void Dispose(bool disposing)
      {
      if (!this.disposed)
      {
      if (disposing)
      {
      // Dispose managed resources.
      }
      // Dispose unmanaged resources.
      }
      disposed = true;
      }

      When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:

      public void Dispose()
      {
      Dispose(true);
      GC.SuppressFinalize(this);
      }

      the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Because you have no idea when the GC will be called, and the Dispose pattern is used to free unmanaged resources like bitmaps or database connections, so you can count on them becoming available right away.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      1 Reply Last reply
      0
      • C cateyes99

        When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:

        protected virtual void Dispose(bool disposing)
        {
        if (!this.disposed)
        {
        if (disposing)
        {
        // Dispose managed resources.
        }
        // Dispose unmanaged resources.
        }
        disposed = true;
        }

        When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:

        public void Dispose()
        {
        Dispose(true);
        GC.SuppressFinalize(this);
        }

        the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        cateyes_99 wrote:

        Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time.

        But what about other managed objects that wrap unmanaged resources. Things like File, Pen etc.? For example :-

        class TheDisposable : IDisposable
        {
        IntPtr handle; //Direct unmanaged resource
        FileStream stream;//Managed but wraps an unmanaged resource

        public void Dispose
        {
        Dispose(true);
        GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
        if (disposing)
        {
        stream.Dispose();
        }
        CloseHandle(handle);
        }

        ~TheDisposable() { Dispose(false); }
        }

        If you don't explicitly dispose the stream here, you'd have to wait for the finalizer for that particular FileStream object to run (assuming it has a finalizer). Like you said, there is no need (usually) to set references to null in Dispose, as the GC is intelligent enough to figure out that an object has no live references.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        C 1 Reply Last reply
        0
        • S S Senthil Kumar

          cateyes_99 wrote:

          Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time.

          But what about other managed objects that wrap unmanaged resources. Things like File, Pen etc.? For example :-

          class TheDisposable : IDisposable
          {
          IntPtr handle; //Direct unmanaged resource
          FileStream stream;//Managed but wraps an unmanaged resource

          public void Dispose
          {
          Dispose(true);
          GC.SuppressFinalize(this);
          }

          protected virtual void Dispose(bool disposing)
          {
          if (disposing)
          {
          stream.Dispose();
          }
          CloseHandle(handle);
          }

          ~TheDisposable() { Dispose(false); }
          }

          If you don't explicitly dispose the stream here, you'd have to wait for the finalizer for that particular FileStream object to run (assuming it has a finalizer). Like you said, there is no need (usually) to set references to null in Dispose, as the GC is intelligent enough to figure out that an object has no live references.

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          C Offline
          C Offline
          cateyes99
          wrote on last edited by
          #4

          Thanks. It makes much clear

          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