When should I use 'using' ?

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

    #1

    When should I use 'using' ?

    Hi !

    I'm wondering about the use of the 'using' statement.
    In a procedure, should i declare all instance variables in a using
    clause ?

    In the below code, what would happen if MyFont & MyFont2 would be
    declared in the body of the Main() and not inside the using (even not
    referenced in the using) ? Would be there something to call to dispose
    them ?

    I thought the garbage collector was doing all this by itself...



    ***********
    code sample
    ***********
    using System.Drawing;
    class a
    {
    public static void Main()
    {
    using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font
    ("Arial", 10.0f))
    {
    // use MyFont and MyFont2
    } // compiler will call Dispose on MyFont and MyFont2

    Font MyFont3 = new Font("Arial", 10.0f);
    using (MyFont3) // WHAT HAPPEN IF no using here ???
    {
    // use MyFont3
    } // compiler will call Dispose on MyFont3

    }
    }





    Regards,
    Cybertof.
  • Greg Ewing [MVP]

    #2
    Re: When should I use 'using' ?

    Cybertof, if you the using block in this case is taking care of calling the
    Dispose method as you have indicated. If you didn't use the using statement
    in your second code section you would just have to call Dispose yourself.
    In that case you should wrap the Font creation and use in a try...
    finally... block to ensure that your Font is Disposed properly. I would
    recommend the using statement though since it takes care of making sure your
    object is cleaned up. Does that answer your question?

    --
    Greg Ewing [MVP]




    "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
    news:MPG.19ef8f c02fd870bc9896a 2@msnews.micros oft.com...[color=blue]
    > Hi !
    >
    > I'm wondering about the use of the 'using' statement.
    > In a procedure, should i declare all instance variables in a using
    > clause ?
    >
    > In the below code, what would happen if MyFont & MyFont2 would be
    > declared in the body of the Main() and not inside the using (even not
    > referenced in the using) ? Would be there something to call to dispose
    > them ?
    >
    > I thought the garbage collector was doing all this by itself...
    >
    >
    >
    > ***********
    > code sample
    > ***********
    > using System.Drawing;
    > class a
    > {
    > public static void Main()
    > {
    > using (Font MyFont = new Font("Arial", 10.0f), MyFont2 = new Font
    > ("Arial", 10.0f))
    > {
    > // use MyFont and MyFont2
    > } // compiler will call Dispose on MyFont and MyFont2
    >
    > Font MyFont3 = new Font("Arial", 10.0f);
    > using (MyFont3) // WHAT HAPPEN IF no using here ???
    > {
    > // use MyFont3
    > } // compiler will call Dispose on MyFont3
    >
    > }
    > }
    >
    >
    >
    >
    >
    > Regards,
    > Cybertof.[/color]


    Comment

    • Cybertof

      #3
      Re: When should I use 'using' ?

      Yes, but so i have another question :
      What happens if i don't use 'using' at all in the following code :

      public void Test()
      {
      MessageBox.Show ("Before");
      Font MyFont = new Font("Arial", 10.0f);
      MessageBox.Show ("After");
      }

      MyFont is never released ?
      Does it mean that if you instanciate an object from a class in a
      procedure body, the object is never cleaned ?
      I thought the garbage collector was doing it...
      So should i call .Dispose() on every instance i would create from system
      classes or homemade classes ?


      Regards,
      Cybertof.


      In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
      gewing@_NO_SPAM _claritycon.com says...[color=blue]
      > Cybertof, if you the using block in this case is taking care of calling the
      > Dispose method as you have indicated. If you didn't use the using statement
      > in your second code section you would just have to call Dispose yourself.
      > In that case you should wrap the Font creation and use in a try...
      > finally... block to ensure that your Font is Disposed properly. I would
      > recommend the using statement though since it takes care of making sure your
      > object is cleaned up. Does that answer your question?
      >
      >[/color]

      Comment

      • Bob Powell [MVP]

        #4
        Re: When should I use 'using' ?

        MyFont will be released at the leisure of the GC. Possibly when the program
        closes. You won't get a leak and for the example you cite performance will
        not be important.

        If you do a lot of drawing or use a lot of fonts or just want to be a good
        programmer call Dispose() on all objects that support IDisposable.

        The object finalizer which is called by the GC will ( *should* ) call
        Dispose when it gets collected but unless you call GC.Collect specifically,
        you have no control over the process.

        Personally, I never use using{ .... } I guess its a style choice.

        --
        Bob Powell [MVP]
        C#, System.Drawing

        September's edition of Well Formed is now available.


        Answer those GDI+ questions with the GDI+ FAQ




        "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
        news:MPG.19ef98 332252ca4b9896a 3@msnews.micros oft.com...[color=blue]
        > Yes, but so i have another question :
        > What happens if i don't use 'using' at all in the following code :
        >
        > public void Test()
        > {
        > MessageBox.Show ("Before");
        > Font MyFont = new Font("Arial", 10.0f);
        > MessageBox.Show ("After");
        > }
        >
        > MyFont is never released ?
        > Does it mean that if you instanciate an object from a class in a
        > procedure body, the object is never cleaned ?
        > I thought the garbage collector was doing it...
        > So should i call .Dispose() on every instance i would create from system
        > classes or homemade classes ?
        >
        >
        > Regards,
        > Cybertof.
        >
        >
        > In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
        > gewing@_NO_SPAM _claritycon.com says...[color=green]
        > > Cybertof, if you the using block in this case is taking care of calling[/color][/color]
        the[color=blue][color=green]
        > > Dispose method as you have indicated. If you didn't use the using[/color][/color]
        statement[color=blue][color=green]
        > > in your second code section you would just have to call Dispose[/color][/color]
        yourself.[color=blue][color=green]
        > > In that case you should wrap the Font creation and use in a try...
        > > finally... block to ensure that your Font is Disposed properly. I would
        > > recommend the using statement though since it takes care of making sure[/color][/color]
        your[color=blue][color=green]
        > > object is cleaned up. Does that answer your question?
        > >
        > >[/color][/color]


        Comment

        • Cybertof

          #5
          Re: ArrayList Pointers (+ GC + Dispose)

          So i'm in front of a big question :

          Does the ArrayList.Add() method just add a reference to the object added
          or the object is copied ?
          The answer seems 'reference' as if i do the following code i get the
          same content for everyobject added :

          So let's consider the following code :

          // i'm in a class constructor
          foreach (DataRow dataRow in dataTable.Rows)
          {
          CLockeInstrumen t myInstrument=ne w CLockeInstrumen t(dataRow);
          MyArrayList.Add (myInstrument);
          }

          //Notes :
          //- The constructor initialise the instance with columns of the dataRow.
          //- MyArrayList is an ArrayList declared in my Class


          If outside the foreach i 'dispose' the myInstrument object,
          what happens to the ArrayList content ?
          (note that myInstrument instance may not exist anymore outside the
          constructor, and the private class memeber MyArrayList will...)






          In article <#1RmUnnjDHA.25 80@TK2MSFTNGP10 .phx.gbl>,
          bob@_spamkiller _bobpowell.net says...[color=blue]
          > MyFont will be released at the leisure of the GC. Possibly when the program
          > closes. You won't get a leak and for the example you cite performance will
          > not be important.
          >
          > If you do a lot of drawing or use a lot of fonts or just want to be a good
          > programmer call Dispose() on all objects that support IDisposable.
          >
          > The object finalizer which is called by the GC will ( *should* ) call
          > Dispose when it gets collected but unless you call GC.Collect specifically,
          > you have no control over the process.
          >
          > Personally, I never use using{ .... } I guess its a style choice.
          >[/color]

          Comment

          • Mr.Tickle

            #6
            Re: When should I use 'using' ?

            You cant leak managed objects, you may however leak unmanaged resources.


            "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
            news:#1RmUnnjDH A.2580@TK2MSFTN GP10.phx.gbl...[color=blue]
            > MyFont will be released at the leisure of the GC. Possibly when the[/color]
            program[color=blue]
            > closes. You won't get a leak and for the example you cite performance will
            > not be important.
            >
            > If you do a lot of drawing or use a lot of fonts or just want to be a good
            > programmer call Dispose() on all objects that support IDisposable.
            >
            > The object finalizer which is called by the GC will ( *should* ) call
            > Dispose when it gets collected but unless you call GC.Collect[/color]
            specifically,[color=blue]
            > you have no control over the process.
            >
            > Personally, I never use using{ .... } I guess its a style choice.
            >
            > --
            > Bob Powell [MVP]
            > C#, System.Drawing
            >
            > September's edition of Well Formed is now available.
            > http://www.bobpowell.net/currentissue.htm
            >
            > Answer those GDI+ questions with the GDI+ FAQ
            > http://www.bobpowell.net/gdiplus_faq.htm
            >
            >
            >
            > "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
            > news:MPG.19ef98 332252ca4b9896a 3@msnews.micros oft.com...[color=green]
            > > Yes, but so i have another question :
            > > What happens if i don't use 'using' at all in the following code :
            > >
            > > public void Test()
            > > {
            > > MessageBox.Show ("Before");
            > > Font MyFont = new Font("Arial", 10.0f);
            > > MessageBox.Show ("After");
            > > }
            > >
            > > MyFont is never released ?
            > > Does it mean that if you instanciate an object from a class in a
            > > procedure body, the object is never cleaned ?
            > > I thought the garbage collector was doing it...
            > > So should i call .Dispose() on every instance i would create from system
            > > classes or homemade classes ?
            > >
            > >
            > > Regards,
            > > Cybertof.
            > >
            > >
            > > In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
            > > gewing@_NO_SPAM _claritycon.com says...[color=darkred]
            > > > Cybertof, if you the using block in this case is taking care of[/color][/color][/color]
            calling[color=blue]
            > the[color=green][color=darkred]
            > > > Dispose method as you have indicated. If you didn't use the using[/color][/color]
            > statement[color=green][color=darkred]
            > > > in your second code section you would just have to call Dispose[/color][/color]
            > yourself.[color=green][color=darkred]
            > > > In that case you should wrap the Font creation and use in a try...
            > > > finally... block to ensure that your Font is Disposed properly. I[/color][/color][/color]
            would[color=blue][color=green][color=darkred]
            > > > recommend the using statement though since it takes care of making[/color][/color][/color]
            sure[color=blue]
            > your[color=green][color=darkred]
            > > > object is cleaned up. Does that answer your question?
            > > >
            > > >[/color][/color]
            >
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: ArrayList Pointers (+ GC + Dispose)

              Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
              > So i'm in front of a big question :
              >
              > Does the ArrayList.Add() method just add a reference to the object added
              > or the object is copied ?[/color]

              The reference is copied.
              [color=blue]
              > The answer seems 'reference' as if i do the following code i get the
              > same content for everyobject added :
              >
              > So let's consider the following code :
              >
              > // i'm in a class constructor
              > foreach (DataRow dataRow in dataTable.Rows)
              > {
              > CLockeInstrumen t myInstrument=ne w CLockeInstrumen t(dataRow);
              > MyArrayList.Add (myInstrument);
              > }[/color]

              Okay.
              [color=blue]
              > //Notes :
              > //- The constructor initialise the instance with columns of the dataRow.
              > //- MyArrayList is an ArrayList declared in my Class
              >
              >
              > If outside the foreach i 'dispose' the myInstrument object,[/color]

              Which myInstrument object? You're creating and adding lots of them.
              [color=blue]
              > what happens to the ArrayList content ?[/color]

              The object is disposed, but the reference is still to a live object -
              it's just that you can't do anything with it without getting an
              exception.
              [color=blue]
              > (note that myInstrument instance may not exist anymore outside the
              > constructor, and the private class memeber MyArrayList will...)[/color]

              I think you need to be very clear about the difference between
              instances and variables. A variable can hold a reference to an
              instance, but it isn't an instance itself.

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

              • 100

                #8
                Re: When should I use 'using' ?

                Mr.Tickle,

                To be more strict one can't leek unmanaged resources either if the class is
                carefully designed and implemented (classes in the framework has to be so,
                but some of them are not) . If the class holds unmanaged resources it has to
                have *Finalize* method declared. Actually it doesn't have to, but this is by
                dispose pattern. The *Finalize* method has to call Dispose and free the
                unmanaged resources.
                In this case the unmanaged resources are even released before the memory is
                garbage collected (the memory will be collected on the next GC request.)

                B\rgds
                100


                "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                news:eX9MC2njDH A.2200@TK2MSFTN GP12.phx.gbl...[color=blue]
                > You cant leak managed objects, you may however leak unmanaged resources.
                >
                >
                > "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
                > news:#1RmUnnjDH A.2580@TK2MSFTN GP10.phx.gbl...[color=green]
                > > MyFont will be released at the leisure of the GC. Possibly when the[/color]
                > program[color=green]
                > > closes. You won't get a leak and for the example you cite performance[/color][/color]
                will[color=blue][color=green]
                > > not be important.
                > >
                > > If you do a lot of drawing or use a lot of fonts or just want to be a[/color][/color]
                good[color=blue][color=green]
                > > programmer call Dispose() on all objects that support IDisposable.
                > >
                > > The object finalizer which is called by the GC will ( *should* ) call
                > > Dispose when it gets collected but unless you call GC.Collect[/color]
                > specifically,[color=green]
                > > you have no control over the process.
                > >
                > > Personally, I never use using{ .... } I guess its a style choice.
                > >
                > > --
                > > Bob Powell [MVP]
                > > C#, System.Drawing
                > >
                > > September's edition of Well Formed is now available.
                > > http://www.bobpowell.net/currentissue.htm
                > >
                > > Answer those GDI+ questions with the GDI+ FAQ
                > > http://www.bobpowell.net/gdiplus_faq.htm
                > >
                > >
                > >
                > > "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                > > news:MPG.19ef98 332252ca4b9896a 3@msnews.micros oft.com...[color=darkred]
                > > > Yes, but so i have another question :
                > > > What happens if i don't use 'using' at all in the following code :
                > > >
                > > > public void Test()
                > > > {
                > > > MessageBox.Show ("Before");
                > > > Font MyFont = new Font("Arial", 10.0f);
                > > > MessageBox.Show ("After");
                > > > }
                > > >
                > > > MyFont is never released ?
                > > > Does it mean that if you instanciate an object from a class in a
                > > > procedure body, the object is never cleaned ?
                > > > I thought the garbage collector was doing it...
                > > > So should i call .Dispose() on every instance i would create from[/color][/color][/color]
                system[color=blue][color=green][color=darkred]
                > > > classes or homemade classes ?
                > > >
                > > >
                > > > Regards,
                > > > Cybertof.
                > > >
                > > >
                > > > In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
                > > > gewing@_NO_SPAM _claritycon.com says...
                > > > > Cybertof, if you the using block in this case is taking care of[/color][/color]
                > calling[color=green]
                > > the[color=darkred]
                > > > > Dispose method as you have indicated. If you didn't use the using[/color]
                > > statement[color=darkred]
                > > > > in your second code section you would just have to call Dispose[/color]
                > > yourself.[color=darkred]
                > > > > In that case you should wrap the Font creation and use in a try...
                > > > > finally... block to ensure that your Font is Disposed properly. I[/color][/color]
                > would[color=green][color=darkred]
                > > > > recommend the using statement though since it takes care of making[/color][/color]
                > sure[color=green]
                > > your[color=darkred]
                > > > > object is cleaned up. Does that answer your question?
                > > > >
                > > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Mr.Tickle

                  #9
                  Re: When should I use 'using' ?

                  Thats a big IF, and we all know how well people handle resources.

                  Just about as well as they can parse strings.

                  "100" <100@100.com> wrote in message
                  news:#9ExpiojDH A.372@TK2MSFTNG P11.phx.gbl...[color=blue]
                  > Mr.Tickle,
                  >
                  > To be more strict one can't leek unmanaged resources either if the class[/color]
                  is[color=blue]
                  > carefully designed and implemented (classes in the framework has to be so,
                  > but some of them are not) . If the class holds unmanaged resources it has[/color]
                  to[color=blue]
                  > have *Finalize* method declared. Actually it doesn't have to, but this is[/color]
                  by[color=blue]
                  > dispose pattern. The *Finalize* method has to call Dispose and free the
                  > unmanaged resources.
                  > In this case the unmanaged resources are even released before the memory[/color]
                  is[color=blue]
                  > garbage collected (the memory will be collected on the next GC request.)
                  >
                  > B\rgds
                  > 100
                  >
                  >
                  > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                  > news:eX9MC2njDH A.2200@TK2MSFTN GP12.phx.gbl...[color=green]
                  > > You cant leak managed objects, you may however leak unmanaged resources.
                  > >
                  > >
                  > > "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
                  > > news:#1RmUnnjDH A.2580@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  > > > MyFont will be released at the leisure of the GC. Possibly when the[/color]
                  > > program[color=darkred]
                  > > > closes. You won't get a leak and for the example you cite performance[/color][/color]
                  > will[color=green][color=darkred]
                  > > > not be important.
                  > > >
                  > > > If you do a lot of drawing or use a lot of fonts or just want to be a[/color][/color]
                  > good[color=green][color=darkred]
                  > > > programmer call Dispose() on all objects that support IDisposable.
                  > > >
                  > > > The object finalizer which is called by the GC will ( *should* ) call
                  > > > Dispose when it gets collected but unless you call GC.Collect[/color]
                  > > specifically,[color=darkred]
                  > > > you have no control over the process.
                  > > >
                  > > > Personally, I never use using{ .... } I guess its a style choice.
                  > > >
                  > > > --
                  > > > Bob Powell [MVP]
                  > > > C#, System.Drawing
                  > > >
                  > > > September's edition of Well Formed is now available.
                  > > > http://www.bobpowell.net/currentissue.htm
                  > > >
                  > > > Answer those GDI+ questions with the GDI+ FAQ
                  > > > http://www.bobpowell.net/gdiplus_faq.htm
                  > > >
                  > > >
                  > > >
                  > > > "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                  > > > news:MPG.19ef98 332252ca4b9896a 3@msnews.micros oft.com...
                  > > > > Yes, but so i have another question :
                  > > > > What happens if i don't use 'using' at all in the following code :
                  > > > >
                  > > > > public void Test()
                  > > > > {
                  > > > > MessageBox.Show ("Before");
                  > > > > Font MyFont = new Font("Arial", 10.0f);
                  > > > > MessageBox.Show ("After");
                  > > > > }
                  > > > >
                  > > > > MyFont is never released ?
                  > > > > Does it mean that if you instanciate an object from a class in a
                  > > > > procedure body, the object is never cleaned ?
                  > > > > I thought the garbage collector was doing it...
                  > > > > So should i call .Dispose() on every instance i would create from[/color][/color]
                  > system[color=green][color=darkred]
                  > > > > classes or homemade classes ?
                  > > > >
                  > > > >
                  > > > > Regards,
                  > > > > Cybertof.
                  > > > >
                  > > > >
                  > > > > In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
                  > > > > gewing@_NO_SPAM _claritycon.com says...
                  > > > > > Cybertof, if you the using block in this case is taking care of[/color]
                  > > calling[color=darkred]
                  > > > the
                  > > > > > Dispose method as you have indicated. If you didn't use the using
                  > > > statement
                  > > > > > in your second code section you would just have to call Dispose
                  > > > yourself.
                  > > > > > In that case you should wrap the Font creation and use in a try...
                  > > > > > finally... block to ensure that your Font is Disposed properly. I[/color]
                  > > would[color=darkred]
                  > > > > > recommend the using statement though since it takes care of making[/color]
                  > > sure[color=darkred]
                  > > > your
                  > > > > > object is cleaned up. Does that answer your question?
                  > > > > >
                  > > > > >
                  > > >
                  > > >[/color]
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  • José Joye

                    #10
                    Re: ArrayList Pointers (+ GC + Dispose)

                    Congrats for your new title :-)

                    José
                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> a écrit dans le message de
                    news:MPG.19ef95 6de5a30f5598983 c@msnews.micros oft.com...[color=blue]
                    > Cybertof <cybertof2003no spam@gmx.net> wrote:[color=green]
                    > > So i'm in front of a big question :
                    > >
                    > > Does the ArrayList.Add() method just add a reference to the object added
                    > > or the object is copied ?[/color]
                    >
                    > The reference is copied.
                    >[color=green]
                    > > The answer seems 'reference' as if i do the following code i get the
                    > > same content for everyobject added :
                    > >
                    > > So let's consider the following code :
                    > >
                    > > // i'm in a class constructor
                    > > foreach (DataRow dataRow in dataTable.Rows)
                    > > {
                    > > CLockeInstrumen t myInstrument=ne w CLockeInstrumen t(dataRow);
                    > > MyArrayList.Add (myInstrument);
                    > > }[/color]
                    >
                    > Okay.
                    >[color=green]
                    > > //Notes :
                    > > //- The constructor initialise the instance with columns of the dataRow.
                    > > //- MyArrayList is an ArrayList declared in my Class
                    > >
                    > >
                    > > If outside the foreach i 'dispose' the myInstrument object,[/color]
                    >
                    > Which myInstrument object? You're creating and adding lots of them.
                    >[color=green]
                    > > what happens to the ArrayList content ?[/color]
                    >
                    > The object is disposed, but the reference is still to a live object -
                    > it's just that you can't do anything with it without getting an
                    > exception.
                    >[color=green]
                    > > (note that myInstrument instance may not exist anymore outside the
                    > > constructor, and the private class memeber MyArrayList will...)[/color]
                    >
                    > I think you need to be very clear about the difference between
                    > instances and variables. A variable can hold a reference to an
                    > instance, but it isn't an instance itself.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • 100

                      #11
                      Re: When should I use 'using' ?

                      Anyway, Dispose has to be called as soon as the object is no longer in use.
                      When holding unmanaged resource actually we hold memory which cannot be
                      used. I haven't heard anybody run out of handlers, but I have heard people
                      run out of memory because of that.
                      However most of the classes are not disposable. Normally programmers don't
                      write disposable types. Prgrammers who writes them, though, has to be
                      perfectly familiar with dispose pattern. So I assuming that all disposable
                      types are correctly written. That's why I dare to say that we have resource
                      leaks as much as we have memory leaks.

                      Or at least I hope so :)

                      B\rgds
                      100

                      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      news:uSOkPmojDH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > Thats a big IF, and we all know how well people handle resources.
                      >
                      > Just about as well as they can parse strings.
                      >
                      > "100" <100@100.com> wrote in message
                      > news:#9ExpiojDH A.372@TK2MSFTNG P11.phx.gbl...[color=green]
                      > > Mr.Tickle,
                      > >
                      > > To be more strict one can't leek unmanaged resources either if the class[/color]
                      > is[color=green]
                      > > carefully designed and implemented (classes in the framework has to be[/color][/color]
                      so,[color=blue][color=green]
                      > > but some of them are not) . If the class holds unmanaged resources it[/color][/color]
                      has[color=blue]
                      > to[color=green]
                      > > have *Finalize* method declared. Actually it doesn't have to, but this[/color][/color]
                      is[color=blue]
                      > by[color=green]
                      > > dispose pattern. The *Finalize* method has to call Dispose and free the
                      > > unmanaged resources.
                      > > In this case the unmanaged resources are even released before the memory[/color]
                      > is[color=green]
                      > > garbage collected (the memory will be collected on the next GC request.)
                      > >
                      > > B\rgds
                      > > 100
                      > >
                      > >
                      > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      > > news:eX9MC2njDH A.2200@TK2MSFTN GP12.phx.gbl...[color=darkred]
                      > > > You cant leak managed objects, you may however leak unmanaged[/color][/color][/color]
                      resources.[color=blue][color=green][color=darkred]
                      > > >
                      > > >
                      > > > "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
                      > > > news:#1RmUnnjDH A.2580@TK2MSFTN GP10.phx.gbl...
                      > > > > MyFont will be released at the leisure of the GC. Possibly when the
                      > > > program
                      > > > > closes. You won't get a leak and for the example you cite[/color][/color][/color]
                      performance[color=blue][color=green]
                      > > will[color=darkred]
                      > > > > not be important.
                      > > > >
                      > > > > If you do a lot of drawing or use a lot of fonts or just want to be[/color][/color][/color]
                      a[color=blue][color=green]
                      > > good[color=darkred]
                      > > > > programmer call Dispose() on all objects that support IDisposable.
                      > > > >
                      > > > > The object finalizer which is called by the GC will ( *should* )[/color][/color][/color]
                      call[color=blue][color=green][color=darkred]
                      > > > > Dispose when it gets collected but unless you call GC.Collect
                      > > > specifically,
                      > > > > you have no control over the process.
                      > > > >
                      > > > > Personally, I never use using{ .... } I guess its a style choice.
                      > > > >
                      > > > > --
                      > > > > Bob Powell [MVP]
                      > > > > C#, System.Drawing
                      > > > >
                      > > > > September's edition of Well Formed is now available.
                      > > > > http://www.bobpowell.net/currentissue.htm
                      > > > >
                      > > > > Answer those GDI+ questions with the GDI+ FAQ
                      > > > > http://www.bobpowell.net/gdiplus_faq.htm
                      > > > >
                      > > > >
                      > > > >
                      > > > > "Cybertof" <cybertof2003no spam@gmx.net> wrote in message
                      > > > > news:MPG.19ef98 332252ca4b9896a 3@msnews.micros oft.com...
                      > > > > > Yes, but so i have another question :
                      > > > > > What happens if i don't use 'using' at all in the following code :
                      > > > > >
                      > > > > > public void Test()
                      > > > > > {
                      > > > > > MessageBox.Show ("Before");
                      > > > > > Font MyFont = new Font("Arial", 10.0f);
                      > > > > > MessageBox.Show ("After");
                      > > > > > }
                      > > > > >
                      > > > > > MyFont is never released ?
                      > > > > > Does it mean that if you instanciate an object from a class in a
                      > > > > > procedure body, the object is never cleaned ?
                      > > > > > I thought the garbage collector was doing it...
                      > > > > > So should i call .Dispose() on every instance i would create from[/color]
                      > > system[color=darkred]
                      > > > > > classes or homemade classes ?
                      > > > > >
                      > > > > >
                      > > > > > Regards,
                      > > > > > Cybertof.
                      > > > > >
                      > > > > >
                      > > > > > In article <eiOQ1NnjDHA.24 88@TK2MSFTNGP12 .phx.gbl>,
                      > > > > > gewing@_NO_SPAM _claritycon.com says...
                      > > > > > > Cybertof, if you the using block in this case is taking care of
                      > > > calling
                      > > > > the
                      > > > > > > Dispose method as you have indicated. If you didn't use the[/color][/color][/color]
                      using[color=blue][color=green][color=darkred]
                      > > > > statement
                      > > > > > > in your second code section you would just have to call Dispose
                      > > > > yourself.
                      > > > > > > In that case you should wrap the Font creation and use in a[/color][/color][/color]
                      try...[color=blue][color=green][color=darkred]
                      > > > > > > finally... block to ensure that your Font is Disposed properly.[/color][/color][/color]
                      I[color=blue][color=green][color=darkred]
                      > > > would
                      > > > > > > recommend the using statement though since it takes care of[/color][/color][/color]
                      making[color=blue][color=green][color=darkred]
                      > > > sure
                      > > > > your
                      > > > > > > object is cleaned up. Does that answer your question?
                      > > > > > >
                      > > > > > >
                      > > > >
                      > > > >
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Cybertof

                        #12
                        Re: ArrayList Pointers (+ GC + Dispose)

                        I'm not sure the ArrayList.Add() method make a copy of the object passed
                        to it

                        For example in this case :

                        CLockeInstrumen t myInstrument=ne w CLockeInstrumen t();
                        foreach (DataRow dataRow in dataTable.Rows)
                        {
                        MyInstrument.Fi llFromDataRow(d ataRow);
                        MyArrayList.Add (myInstrument);
                        }

                        In this case, MyInstrument is not declared as 'new' in each loop,
                        and at the end, i get a full MyArrayList will all the same instruments
                        pointing to the same instance (the last one beeing affected in
                        MyInstrument).
                        If the MyArrayList.Add () would make copy, the subsequent call to
                        MyInstrument.Fi llFromDataRow(d ataRow) would not affect previous items
                        from the ArrayList.

                        No ?



                        In article <MPG.19ef956de5 a30f5598983c@ms news.microsoft. com>,
                        skeet@pobox.com says...[color=blue]
                        > Cybertof <cybertof2003no spam@gmx.net> wrote:[color=green]
                        > > So i'm in front of a big question :
                        > >
                        > > Does the ArrayList.Add() method just add a reference to the object added
                        > > or the object is copied ?[/color]
                        >
                        > The reference is copied.
                        >[/color]

                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: ArrayList Pointers (+ GC + Dispose)

                          Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
                          > I'm not sure the ArrayList.Add() method make a copy of the object passed
                          > to it[/color]

                          It doesn't. It makes a copy of the *reference*, as I said.
                          [color=blue]
                          > For example in this case :
                          >
                          > CLockeInstrumen t myInstrument=ne w CLockeInstrumen t();
                          > foreach (DataRow dataRow in dataTable.Rows)
                          > {
                          > MyInstrument.Fi llFromDataRow(d ataRow);
                          > MyArrayList.Add (myInstrument);
                          > }
                          >
                          > In this case, MyInstrument is not declared as 'new' in each loop,
                          > and at the end, i get a full MyArrayList will all the same instruments
                          > pointing to the same instance (the last one beeing affected in
                          > MyInstrument).[/color]

                          Yes. You get lots of copies of the same reference.
                          [color=blue]
                          > If the MyArrayList.Add () would make copy, the subsequent call to
                          > MyInstrument.Fi llFromDataRow(d ataRow) would not affect previous items
                          > from the ArrayList.
                          >
                          > No ?[/color]

                          Yes, but as I said, it's not copying the *object*, it's copying the
                          *reference*. It's very important that you understand how reference
                          types and value types work - I've got a couple of articles which deal
                          with this tangentially, and might help you.

                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


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

                          • Cybertof

                            #14
                            Re: ArrayList Pointers (+ GC + Dispose)

                            Hello Jon,

                            Thanks for your articles.

                            When declaring something like

                            public void Test()
                            {
                            CMyClass MyClassInstance = new CMyClass();
                            }

                            If I understand what your are saying, this line creates a new object on
                            the Heap (the object is an instance of CMyClass) and the variable
                            MyCalssInstance contains the reference to that object.
                            Am i right ?

                            Now, my 2 question is :
                            1- How (and when) is the object on the heap created ?
                            2- How (and by who) should this object be distroyed ?

                            For the 3rd question (and last one i hope), let's go back to my previous
                            sample :

                            foreach (DataRow dataRow in dataTable.Rows)
                            {
                            CLockeInstrumen t myInstrument=ne w CLockeInstrumen t(dataRow);
                            MyArrayList.Add (myInstrument);
                            }

                            This code creates 'on the fly' instances of the CLockeInstrumen t class,
                            each beeing reference in the variable myInstrument.
                            But, each time the loop is performed, the variable is overwritten by a
                            new reference to a new object on the heap.
                            Then, i'm in the case of that i have lost all my references (apart of
                            those beeing stored in the MyArrayList.
                            So, when all the objects (on the heap) will be destroyed ?

                            Does the ArrayList contains the object itselves (a copy) or a list of
                            references to objects on the heap ?


                            Regards,
                            Cybertof.




                            In article <MPG.19f06842c1 bfbf98989846@ms news.microsoft. com>,
                            skeet@pobox.com says...[color=blue]
                            >
                            >I think you need to be very clear about the difference between
                            >instances and variables. A variable can hold a reference to an
                            >instance, but it isn't an instance itself.
                            >
                            > Yes, but as I said, it's not copying the *object*, it's copying the
                            > *reference*. It's very important that you understand how reference
                            > types and value types work - I've got a couple of articles which deal
                            > with this tangentially, and might help you.
                            >
                            > http://www.pobox.com/~skeet/csharp/parameters.html
                            > http://www.pobox.com/~skeet/csharp/memory.html
                            >
                            >[/color]

                            Comment

                            • Jon Skeet [C# MVP]

                              #15
                              Re: ArrayList Pointers (+ GC + Dispose)

                              Cybertof <cybertof2003no spam@gmx.net> wrote:[color=blue]
                              > Thanks for your articles.
                              >
                              > When declaring something like
                              >
                              > public void Test()
                              > {
                              > CMyClass MyClassInstance = new CMyClass();
                              > }
                              >
                              > If I understand what your are saying, this line creates a new object on
                              > the Heap (the object is an instance of CMyClass) and the variable
                              > MyCalssInstance contains the reference to that object.
                              > Am i right ?[/color]

                              Yes.
                              [color=blue]
                              > Now, my 2 question is :
                              > 1- How (and when) is the object on the heap created ?[/color]

                              It's created by you calling new CMyClass(), and it's created at that
                              point.
                              [color=blue]
                              > 2- How (and by who) should this object be distroyed ?[/color]

                              If it doesn't contain any unmanaged resources, the memory it occupies
                              will be released at some point after there are no "live" variables
                              which reference it (directly or indirectly) by the garbage collector,
                              and that's fine.

                              If it contains unmanaged resources, you should implement IDisposable so
                              that those resources can be explicitly released, but the memory for the
                              object itself will still be in the hands of the garbage collector.
                              [color=blue]
                              > For the 3rd question (and last one i hope), let's go back to my previous
                              > sample :
                              >
                              > foreach (DataRow dataRow in dataTable.Rows)
                              > {
                              > CLockeInstrumen t myInstrument=ne w CLockeInstrumen t(dataRow);
                              > MyArrayList.Add (myInstrument);
                              > }
                              >
                              > This code creates 'on the fly' instances of the CLockeInstrumen t class,
                              > each beeing reference in the variable myInstrument.
                              > But, each time the loop is performed, the variable is overwritten by a
                              > new reference to a new object on the heap.
                              > Then, i'm in the case of that i have lost all my references (apart of
                              > those beeing stored in the MyArrayList.
                              > So, when all the objects (on the heap) will be destroyed ?[/color]

                              They will only be eligible for garbage collection when either their
                              "slots" in the ArrayList are replaced, or the ArrayList itself is
                              eligible for garbage collection.
                              [color=blue]
                              > Does the ArrayList contains the object itselves (a copy) or a list of
                              > references to objects on the heap ?[/color]

                              It contains a list of references.

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

                              Working...