Re: " //Clean Up managed resources " f&*ck
- From: "Koliber (js)" <profesor_kinbote@xxxxxxxxxxxxxx>
- Date: 22 May 2007 03:39:26 -0700
On 19 Maj, 16:15, Jesse Houwing <jesse.houw...@xxxxxxxxxxxxxxxx>
wrote:
Hey,
Just though I'd drop in to this discussion.
Basically the only thing left unanswered is the difference between
managed and unmanaged in the context of the .NET framework. The
difference is very easy once you understand it (most things are actually ;))
Managed:
- Anything that is native to the framework. Any class that has been
compiled to .NET Intermediate Language.
Unmanaged:
- Anything that is not native to the framework. Any dll, socket, piece
of memory that has been obtained from the Operating system or a third
party non-.NET dll.
So managed and unmanaged have nothing to do with you having to clean
things up. You can call into unmanaged code (for example a function from
dll that came with Windows) and not have anything to clean up afterwards.
So this leaves the difference between a Managed Resource and a Unmanaged
resource.
Managed Resource
- Any piece of memory or file or pointer that is neatly wrapped in a
.NET object. The .NET SqlConnection object for example. Even if the
programmer does not clean this resource up himself, it will eventually
be cleaned up by the Garbage Collector.
Unmanaged Resource
- Any piece of memory or file or pointer that you have a direct
reference to. So if you have WindowHandle in your .NET Forms control,
you have to make sure it is being cleaned up. These resources will not
be closed ot cleaned up by the Garbage Collector, unless the class
holding on to the resources implements at least a Finalizer.
Now on the issue of why we need a dispose function *and* a finalizer.
While the framework does guarantee it will call the finalizer at some
point, it does not guarantee *when* it will call the finalizer. And - on
top of that - in C# code there is no way to call the finalizer directly.
This is where the Dispose method comes is. This method will allow the
programmer to clean up both the managed and the unmanaged resources held
by the object. You might ask why it is not needed to dispose managed
objects from the finalizer (because that is what the additional boolean
parameter to the Dispose function actually manages). This has to do with
the fact that if teh Garbage collector has decided that the object can
be cleaned up, all it's references must also be ready to be cleaned up.
So the Garbage collector will remove these objects in the next GC run.
If you call Dispose on them however, they're alive again for at least
another garbage collector run. So in order to make sure the objects are
cleaned up as fast as possible, you will not call dispose on the managed
resources.
Jesse
So if I understand there it should be like that
Public class MyClass:IDisposable
{
private bool IsDisposed=false;
public void Dispose()
{
Dispose(true);
GC.SupressFinalize(this);
}
protected void Dispose(bool Diposing)
{
if(!IsDisposed)
{
if(Disposing)
{
//Clean Up managed resources
/////////////////////////////////////////////
aMember.Dispose();
bMember.Dispose();
cMember.Dispose();
////////////////////////////////////////////
}
//Clean up unmanaged resources
////////////////////////////////////////////////
some pinvoke resource releases
for example 'paired' api calls
like this from this page
http://safari.oreilly.com/0321174038/app04
//////////////////////////////////////////////
}
IsDisposed=true;
}
~MyClass()
{
Dispose(false);
}
Is that right?
-----------------
Thanx because if so I have done understand important piece of it.
K.
.
- Follow-Ups:
- Re: Clean Up managed resources
- From: Ben Voigt
- Re: " //Clean Up managed resources " f&*ck
- From: Jesse Houwing
- Re: " //Clean Up managed resources " f&*ck
- From: Christof Nordiek
- Re: Clean up resources.
- From: BLUE
- Re: Clean Up managed resources
- References:
- " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- From: Nicholas Paldino [.NET/C# MVP]
- Re: " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- From: Moty Michaely
- Re: " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- From: Moty Michaely
- Re: " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- From: Moty Michaely
- Re: " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- From: Jesse Houwing
- " //Clean Up managed resources " f&*ck
- Prev by Date: Saving a Dataset
- Next by Date: Re: Add executable to firewall
- Previous by thread: Re: " //Clean Up managed resources " f&*ck
- Next by thread: Re: Clean up resources.
- Index(es):
Relevant Pages
|
Loading