using disposing object

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

    #1

    using disposing object

    Hello!

    I'm using .NET 2005 (C#) and I've come across using statement. It seems ok,
    I was just wondering one thing. What if I returned object inside using
    statement using RETURN directive. Is returned object valid or not?

    For example:

    public FileStream Func1()
    {
    ...
    using (FileStream fs = new FileStream(<som e params>))
    {
    // Do sth with FS
    return fs;
    }
    }

    is on return from Func1 FileStream object returned valid, since it is called
    from inside using statement? Does "using" statement *always* call Dispose,
    or does it just decrement object's reference? If its the first (called
    Dispose), than I must not use using statement for that kind of operations or
    on variables I must return?

    Thanks in advance!

    Best regards,
    Jure


  • Jon Skeet [C# MVP]

    #2
    Re: using disposing object

    On Sep 4, 1:38 pm, "Jure Bogataj" <jure.boga...@m ikrocop.comwrot e:
    I'm using .NET 2005 (C#) and I've come across using statement. It seems ok,
    I was just wondering one thing. What if I returned object inside using
    statement using RETURN directive. Is returned object valid or not?
    That depends on what Dispose does for that types. For some types, you
    can still do some things with them. (A MemoryStream will still let you
    get at its data with ToArray, IIRC).
    Does "using" statement *always* call Dispose,
    or does it just decrement object's reference?
    There's no reference counting in .NET, and Dispose is very separate
    from garbage collection. Dispose is just another method as far as the
    framework is concerned - the IDisposable interface is a convention
    rather than something enforced, although it has language support in C#
    and C++/CLI.
    If its the first (called
    Dispose), than I must not use using statement for that kind of operations or
    on variables I must return?
    Yes, basically.

    Jon

    Comment

    • Laser Lu

      #3
      Re: using disposing object

      IMO, Dispose() method will definitely be called if you created an object
      inside 'using' statement, even if the object is returned as the function
      return value. So, you'd better not return objects that are declared in
      'using' statements.

      "Jure Bogataj" <jure.bogataj@m ikrocop.comwrot e in message
      news:uzmz3Bv7HH A.1164@TK2MSFTN GP02.phx.gbl...
      Hello!
      >
      I'm using .NET 2005 (C#) and I've come across using statement. It seems
      ok, I was just wondering one thing. What if I returned object inside using
      statement using RETURN directive. Is returned object valid or not?
      >
      For example:
      >
      public FileStream Func1()
      {
      ...
      using (FileStream fs = new FileStream(<som e params>))
      {
      // Do sth with FS
      return fs;
      }
      }
      >
      is on return from Func1 FileStream object returned valid, since it is
      called from inside using statement? Does "using" statement *always* call
      Dispose, or does it just decrement object's reference? If its the first
      (called Dispose), than I must not use using statement for that kind of
      operations or on variables I must return?
      >
      Thanks in advance!
      >
      Best regards,
      Jure
      >
      >

      Comment

      • Jure Bogataj

        #4
        Re: using disposing object

        >Does "using" statement *always* call Dispose,
        >or does it just decrement object's reference?
        >
        There's no reference counting in .NET, and Dispose is very separate
        from garbage collection. Dispose is just another method as far as the
        framework is concerned - the IDisposable interface is a convention
        rather than something enforced, although it has language support in C#
        and C++/CLI.
        >
        How come there's no reference counting? Isn't reference counting something
        that GC depends on for destroying objects (e.g. knowing when object can be
        safely destroyed; meaning there exists no reference to it whatsoever).
        And Dispose() is also used with GC is it not?

        Some lightshed on this topic would be greatly appreciated!

        Best regards,
        Jure


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: using disposing object

          On Sep 4, 2:10 pm, "Jure Bogataj" <jure.boga...@m ikrocop.comwrot e:
          There's no reference counting in .NET, and Dispose is very separate
          from garbage collection. Dispose is just another method as far as the
          framework is concerned - the IDisposable interface is a convention
          rather than something enforced, although it has language support in C#
          and C++/CLI.
          >
          How come there's no reference counting? Isn't reference counting something
          that GC depends on for destroying objects (e.g. knowing when object can be
          safely destroyed; meaning there exists no reference to it whatsoever).
          No, the garbage collector uses a mark and sweep algorithm for finding
          live objects, not reference counting. Reference counting generally
          breaks in the face of cyclic references etc, as well as having
          performance issues.
          And Dispose() is also used with GC is it not?
          No, it's not.
          Some lightshed on this topic would be greatly appreciated!
          See http://msdn.microsoft.com/msdnmag/is...t/default.aspx
          for a bit more information. (I haven't read it all to check its
          correctness, but it looks okay.)

          Jon

          Comment

          Working...