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. Will Dispose() be called by GC?

Will Dispose() be called by GC?

Scheduled Pinned Locked Moved C#
csharpdotnetcomperformancehelp
8 Posts 5 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
    Chesnokov Yuriy
    wrote on last edited by
    #1

    Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?

    class My
    {
    public void Dispose()
    {
    // ...
    }
    }

    I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.

    Чесноков

    M L P P 4 Replies Last reply
    0
    • C Chesnokov Yuriy

      Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?

      class My
      {
      public void Dispose()
      {
      // ...
      }
      }

      I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.

      Чесноков

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The runtime won't call it, but will it be confusing to future people looking at the code?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?

        class My
        {
        public void Dispose()
        {
        // ...
        }
        }

        I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.

        Чесноков

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The GC calls Dispose methods only on IDisposable objects. So, in your case GC will not call the Dispose method. Although it is not illegal to use the name 'Dispose' for your cleanup methods, other developers who work with your code may get confused. EDIT: I meant to say the that the GC calls the finalizer which in turn is supposed to call Dispose method. The GC does NOT call Dispose method directly even if the class implements IDisposable interface. However, in a using code block, the C# compiler checks if the class implements IDisposable interface when compiling the code into a try...finally... block and calls its Dispose method in the finally block.

        modified on Wednesday, May 18, 2011 9:44 AM

        1 Reply Last reply
        0
        • C Chesnokov Yuriy

          Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?

          class My
          {
          public void Dispose()
          {
          // ...
          }
          }

          I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.

          Чесноков

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          As it stands, Dispose is just a method name. The trick with IDisposable objects is that the interface is well known, so it can be accessed using (effectively):

          IDisposable = thisClass as IDisposable;
          if (thisClass != null)
          thisClass.Dispose();

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          L 1 Reply Last reply
          0
          • P Pete OHanlon

            As it stands, Dispose is just a method name. The trick with IDisposable objects is that the interface is well known, so it can be accessed using (effectively):

            IDisposable = thisClass as IDisposable;
            if (thisClass != null)
            thisClass.Dispose();

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Shouldn't the code be something like this?

            IDisposable i = thisClass as IDisposable;
            if (i != null) {
            i.Dispose();
            }

            P 1 Reply Last reply
            0
            • C Chesnokov Yuriy

              Is there a chance for a Dispose() method to be called by GC if class is not inhereted from IDisposable?

              class My
              {
              public void Dispose()
              {
              // ...
              }
              }

              I provided that method to immediatly release some memory resources for the class. I wonder if it is prohibited to name some functions after well known methods as Dispose? I experience the following problem Windows service stable APPCRASH in clr.dll after 3.5 to 4.0 .NET platform change[^] which occur in windows service application only.

              Чесноков

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Maybe, if it's called from the finalizer. But I would implement the IDisposable interface, it's free.

              1 Reply Last reply
              0
              • L Lost User

                Shouldn't the code be something like this?

                IDisposable i = thisClass as IDisposable;
                if (i != null) {
                i.Dispose();
                }

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                It should. That's what I get for typing replies on a phone.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                L 1 Reply Last reply
                0
                • P Pete OHanlon

                  It should. That's what I get for typing replies on a phone.

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Never mind, as long as it conveys the idea. But beginners might have a problem though.

                  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