using disposing object

  • Thread starter Thread starter Jure Bogataj
  • Start date Start date
J

Jure Bogataj

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(<some 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
 
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
 
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.
 
Does "using" statement *always* call Dispose,
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
 
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/issues/07/07/CLRInsideOut/default.aspx
for a bit more information. (I haven't read it all to check its
correctness, but it looks okay.)

Jon
 
Back
Top