Dispose()
-
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
-
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
-
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
The reason is that you have declared
c
outside theusing
statement. TheDispose
method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference toc
after leaving theusing
block, theGC
does not collectc
and consequentlyDispose
is called only whenMain
goes out of scope. But even if you had declaredc
within theusing
statement, you are not garantueed to see the expected order of events - theGC
is invoked at indeterminate points. You must useGC.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
-
The reason is that you have declared
c
outside theusing
statement. TheDispose
method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference toc
after leaving theusing
block, theGC
does not collectc
and consequentlyDispose
is called only whenMain
goes out of scope. But even if you had declaredc
within theusing
statement, you are not garantueed to see the expected order of events - theGC
is invoked at indeterminate points. You must useGC.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
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[^].
-
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
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.
-
The reason is that you have declared
c
outside theusing
statement. TheDispose
method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference toc
after leaving theusing
block, theGC
does not collectc
and consequentlyDispose
is called only whenMain
goes out of scope. But even if you had declaredc
within theusing
statement, you are not garantueed to see the expected order of events - theGC
is invoked at indeterminate points. You must useGC.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
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.
-
The reason is that you have declared
c
outside theusing
statement. TheDispose
method is called by the Garbage Collector when collecting objects that are not longer referenced. Because you are still holding a reference toc
after leaving theusing
block, theGC
does not collectc
and consequentlyDispose
is called only whenMain
goes out of scope. But even if you had declaredc
within theusing
statement, you are not garantueed to see the expected order of events - theGC
is invoked at indeterminate points. You must useGC.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
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
-
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
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
-
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
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