Dispose pattern [modified]
-
When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Dispose unmanaged resources.
}
disposed = true;
}When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007
-
When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Dispose unmanaged resources.
}
disposed = true;
}When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007
Because you have no idea when the GC will be called, and the Dispose pattern is used to free unmanaged resources like bitmaps or database connections, so you can count on them becoming available right away.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
When we implement the dispose pattern, in protected virtual void Dispose(bool disposing), we need to say if disposing is true, then dispose managed resources as below:
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
// Dispose managed resources.
}
// Dispose unmanaged resources.
}
disposed = true;
}When we make a call explicitly to this method with disposing = true, in public void Dispose() as below:
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}the managed resources will be explicitly disposed. But why do we need to dispose managed resources explicitly in protected virtual void Dispose(bool disposing). Won't they be disposed automatically by the GC finally after the object reach the end of its life? Secondly, since those are managed resources, how can we get our own hands on to release them? For example, if we allocate some chuncks of memory on heap (i.e, instantiate classes), is there a way for us to release them explicitly? (Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time. And this is actually unnecessary. Right?) Thanx a lot. -- modified at 20:00 Tuesday 5th June, 2007
cateyes_99 wrote:
Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time.
But what about other managed objects that wrap unmanaged resources. Things like File, Pen etc.? For example :-
class TheDisposable : IDisposable
{
IntPtr handle; //Direct unmanaged resource
FileStream stream;//Managed but wraps an unmanaged resourcepublic void Dispose
{
Dispose(true);
GC.SuppressFinalize(this);
}protected virtual void Dispose(bool disposing)
{
if (disposing)
{
stream.Dispose();
}
CloseHandle(handle);
}~TheDisposable() { Dispose(false); }
}If you don't explicitly dispose the stream here, you'd have to wait for the finalizer for that particular FileStream object to run (assuming it has a finalizer). Like you said, there is no need (usually) to set references to null in Dispose, as the GC is intelligent enough to figure out that an object has no live references.
Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
cateyes_99 wrote:
Setting the references to nothing doesn't mean that, it just tells .Net that we don't need them any more, they can be recollected from this point of time.
But what about other managed objects that wrap unmanaged resources. Things like File, Pen etc.? For example :-
class TheDisposable : IDisposable
{
IntPtr handle; //Direct unmanaged resource
FileStream stream;//Managed but wraps an unmanaged resourcepublic void Dispose
{
Dispose(true);
GC.SuppressFinalize(this);
}protected virtual void Dispose(bool disposing)
{
if (disposing)
{
stream.Dispose();
}
CloseHandle(handle);
}~TheDisposable() { Dispose(false); }
}If you don't explicitly dispose the stream here, you'd have to wait for the finalizer for that particular FileStream object to run (assuming it has a finalizer). Like you said, there is no need (usually) to set references to null in Dispose, as the GC is intelligent enough to figure out that an object has no live references.
Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro