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

Dispose()

Scheduled Pinned Locked Moved C#
questionlearning
9 Posts 7 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.
  • M Offline
    M Offline
    Member 781610
    wrote on last edited by
    #1

    Hi, Plz go thro the snippet below.

    class C : IDisposable
    {
    double d;
    public C()
    {
    d = 1.999;
    }
    public void UseLimitedResource()
    {
    Console.WriteLine("Using limited resource...");
    }

            void IDisposable.Dispose()
            {
                Console.WriteLine("Disposing limited resource.");
            }
    
            public void getvalue()
            {
                Console.WriteLine(d.ToString());
            }
        }
    

    class DeployResource
    {
    static void Main(string[] args)
    {
    C c;
    using (c = new C())
    {
    c.UseLimitedResource();

            }
            c.getvalue();
    
            Console.WriteLine("Now Outside using statement.");
    

    }

    Output : Using limited resource... Disposing limited resource. 1.999 Now Outside using statement. Now my question is does an object be called even after it is disposed ? i.e. after calling the dispose still i'm able to get the value 1.999. Can any one explain clearly ? :-O Thanx ;)

    Long Live

    J T G S 4 Replies Last reply
    0
    • M Member 781610

      Hi, Plz go thro the snippet below.

      class C : IDisposable
      {
      double d;
      public C()
      {
      d = 1.999;
      }
      public void UseLimitedResource()
      {
      Console.WriteLine("Using limited resource...");
      }

              void IDisposable.Dispose()
              {
                  Console.WriteLine("Disposing limited resource.");
              }
      
              public void getvalue()
              {
                  Console.WriteLine(d.ToString());
              }
          }
      

      class DeployResource
      {
      static void Main(string[] args)
      {
      C c;
      using (c = new C())
      {
      c.UseLimitedResource();

              }
              c.getvalue();
      
              Console.WriteLine("Now Outside using statement.");
      

      }

      Output : Using limited resource... Disposing limited resource. 1.999 Now Outside using statement. Now my question is does an object be called even after it is disposed ? i.e. after calling the dispose still i'm able to get the value 1.999. Can any one explain clearly ? :-O Thanx ;)

      Long Live

      J Offline
      J Offline
      JoeSharp
      wrote on last edited by
      #2

      yes, because the C object is defined outside the using block. using(C c = new C()) { // your code } // after this line the C object is not accesible

      S 1 Reply Last reply
      0
      • M Member 781610

        Hi, Plz go thro the snippet below.

        class C : IDisposable
        {
        double d;
        public C()
        {
        d = 1.999;
        }
        public void UseLimitedResource()
        {
        Console.WriteLine("Using limited resource...");
        }

                void IDisposable.Dispose()
                {
                    Console.WriteLine("Disposing limited resource.");
                }
        
                public void getvalue()
                {
                    Console.WriteLine(d.ToString());
                }
            }
        

        class DeployResource
        {
        static void Main(string[] args)
        {
        C c;
        using (c = new C())
        {
        c.UseLimitedResource();

                }
                c.getvalue();
        
                Console.WriteLine("Now Outside using statement.");
        

        }

        Output : Using limited resource... Disposing limited resource. 1.999 Now Outside using statement. Now my question is does an object be called even after it is disposed ? i.e. after calling the dispose still i'm able to get the value 1.999. Can any one explain clearly ? :-O Thanx ;)

        Long Live

        T Offline
        T Offline
        Thomas Weller 0
        wrote on last edited by
        #3

        The reason is that you have declared c outside the using statement. The Dispose method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference to c after leaving the using block, the GC does not collect c and consequently Dispose is called only when Main goes out of scope. But even if you had declared c within the using statement, you are not garantueed to see the expected order of events - the GC is invoked at indeterminate points. You must use GC.Collect if you want to be sure:

        using(C c = new C())
        {
        c.UseLimitedResource();
        GC.Collect();
        }

        Regards Thomas

        www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
        Programmer - an organism that turns coffee into software.

        modified on Friday, November 21, 2008 5:26 AM

        M L S 3 Replies Last reply
        0
        • T Thomas Weller 0

          The reason is that you have declared c outside the using statement. The Dispose method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference to c after leaving the using block, the GC does not collect c and consequently Dispose is called only when Main goes out of scope. But even if you had declared c within the using statement, you are not garantueed to see the expected order of events - the GC is invoked at indeterminate points. You must use GC.Collect if you want to be sure:

          using(C c = new C())
          {
          c.UseLimitedResource();
          GC.Collect();
          }

          Regards Thomas

          www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
          Programmer - an organism that turns coffee into software.

          modified on Friday, November 21, 2008 5:26 AM

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          You are wrong: the purpose of the using keyword is exactly to call the Dispose method at the end of a scope. The misunderstanding here is that Dispose has to be used to release unmanage resources used by an object, not to release the object itself. So, if you declare an object outside an using scope, the object and its (managed) properties are still accessible. Try rewriting the OP example using a FileStream instead to a custom class: outside the using scope the stream object is still accessible, but you can no longer use it to read or write data. All is explained, as usually, in MSDN[^].

          1 Reply Last reply
          0
          • M Member 781610

            Hi, Plz go thro the snippet below.

            class C : IDisposable
            {
            double d;
            public C()
            {
            d = 1.999;
            }
            public void UseLimitedResource()
            {
            Console.WriteLine("Using limited resource...");
            }

                    void IDisposable.Dispose()
                    {
                        Console.WriteLine("Disposing limited resource.");
                    }
            
                    public void getvalue()
                    {
                        Console.WriteLine(d.ToString());
                    }
                }
            

            class DeployResource
            {
            static void Main(string[] args)
            {
            C c;
            using (c = new C())
            {
            c.UseLimitedResource();

                    }
                    c.getvalue();
            
                    Console.WriteLine("Now Outside using statement.");
            

            }

            Output : Using limited resource... Disposing limited resource. 1.999 Now Outside using statement. Now my question is does an object be called even after it is disposed ? i.e. after calling the dispose still i'm able to get the value 1.999. Can any one explain clearly ? :-O Thanx ;)

            Long Live

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            There is nothing magical about the Dispose method. If the Dispose method doesn't do anything to make the object unusuable, it's still usable after the Dispose method has been called. I think that you are confusing disposal with garbage collection. The object can be collected when it's not used any more, and there is no direct connection between disposal and garbage collection. The IDisposable interface is used when there is a need to control the life cycle of an object. There is no destructors in .NET (as in systems that use reference counters instead of garbage collection), i.e. an object is not removed instantly when you remove the last reference to it. The garbage collector collects the object eventually, but you have no control over when that happens, so that can't be used reliably to clean up unmanaged resources. (Eventhough memory is a limited resource, it's managed, so the garbage collector can safely free up large memory structures on it's own.)

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            • T Thomas Weller 0

              The reason is that you have declared c outside the using statement. The Dispose method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference to c after leaving the using block, the GC does not collect c and consequently Dispose is called only when Main goes out of scope. But even if you had declared c within the using statement, you are not garantueed to see the expected order of events - the GC is invoked at indeterminate points. You must use GC.Collect if you want to be sure:

              using(C c = new C())
              {
              c.UseLimitedResource();
              GC.Collect();
              }

              Regards Thomas

              www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
              Programmer - an organism that turns coffee into software.

              modified on Friday, November 21, 2008 5:26 AM

              L Offline
              L Offline
              Le centriste
              wrote on last edited by
              #6

              As the other guy pointed out, the GC does not call Dispose(), because it does know about it. What it knows about, is the finalizer, which usually calls the Dispose() method.

              1 Reply Last reply
              0
              • T Thomas Weller 0

                The reason is that you have declared c outside the using statement. The Dispose method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference to c after leaving the using block, the GC does not collect c and consequently Dispose is called only when Main goes out of scope. But even if you had declared c within the using statement, you are not garantueed to see the expected order of events - the GC is invoked at indeterminate points. You must use GC.Collect if you want to be sure:

                using(C c = new C())
                {
                c.UseLimitedResource();
                GC.Collect();
                }

                Regards Thomas

                www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                Programmer - an organism that turns coffee into software.

                modified on Friday, November 21, 2008 5:26 AM

                S Offline
                S Offline
                Scott Dorman
                wrote on last edited by
                #7

                Thomas Weller wrote:

                You must use GC.Collect if you want to be sure

                No, you shouldn't. Calling GC.Collect is a very bad practice. The using statement translates in to a try/finally block where c.Dispose will be called in the finally.

                Scott Dorman

                Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                1 Reply Last reply
                0
                • M Member 781610

                  Hi, Plz go thro the snippet below.

                  class C : IDisposable
                  {
                  double d;
                  public C()
                  {
                  d = 1.999;
                  }
                  public void UseLimitedResource()
                  {
                  Console.WriteLine("Using limited resource...");
                  }

                          void IDisposable.Dispose()
                          {
                              Console.WriteLine("Disposing limited resource.");
                          }
                  
                          public void getvalue()
                          {
                              Console.WriteLine(d.ToString());
                          }
                      }
                  

                  class DeployResource
                  {
                  static void Main(string[] args)
                  {
                  C c;
                  using (c = new C())
                  {
                  c.UseLimitedResource();

                          }
                          c.getvalue();
                  
                          Console.WriteLine("Now Outside using statement.");
                  

                  }

                  Output : Using limited resource... Disposing limited resource. 1.999 Now Outside using statement. Now my question is does an object be called even after it is disposed ? i.e. after calling the dispose still i'm able to get the value 1.999. Can any one explain clearly ? :-O Thanx ;)

                  Long Live

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  As Guffa said, calling Dispose does not actually release the memory used by the object. It simply helps tell the GC that you're done using the object. The GC will determine when it should be "collected" and the memory reclaimed. Until that happens, the object is still accessible. By the way, you shouldn't explicitly implement Dispose as this doesn't follow the Dispose pattern properly. See this article[^] for more details.

                  Scott Dorman

                  Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                  Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                  1 Reply Last reply
                  0
                  • J JoeSharp

                    yes, because the C object is defined outside the using block. using(C c = new C()) { // your code } // after this line the C object is not accesible

                    S Offline
                    S Offline
                    Scott Dorman
                    wrote on last edited by
                    #9

                    Defining the object outside of the using block has nothing to do with it. The compiler will do this anyway when it converts the using to a try/finally block. The problem is a misunderstanding between disposal and collection. Calling Dispose does not release any managed memory. The GC will do that when it runs a collection and determines that the object is no longer referenced.

                    Scott Dorman

                    Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


                    Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai

                    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