Re: " //Clean Up managed resources " f&*ck



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.


.



Relevant Pages

  • Re: " //Clean Up managed resources " f&*ck
    ... So this leaves the difference between a Managed Resource and a Unmanaged resource. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: " //Clean Up managed resources " f&*ck
    ... Since Dispose ... methods is responsible for cleaning resources, the finalizer code ... it is about 'managed resource'. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: object.close() or object = null?
    ... Dispose method is called either explicitly or implicitly by creating the ... resources, and the reference count of a COM object is an unmanaged resource, ... Failure to dispose IDisposable objects typically doesn't lead to memory ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Pointers?
    ... There are finalizers which are expressed using C++'s destructor syntax. ... object, if it implements a finalizer, it is put on the finalization queue and run at an undetermined time in the future by the GC's finalization thread. ... >Dispose to free resource on demand. ... >safe in case Dispose wasn't called by the programmer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: "Poll" Has C# Generally Replaced C++
    ... I actually tried to put objects into a Listusing C# 2.0, and it didn't automatically call Dispose on T. So List::Dispose doesn't call Dispose for the contained objects. ... What if a single resource has 2 object copies, both handling the same resource, and the actual resource should only be disposed when the last object copy goes out of existence? ... In complex applications resources are stored in containers, in other objects, and they're destructed in a very complex way. ... Or even more so, because C programmers are not used to exceptions, but .NET programmers must think of exception safety. ...
    (microsoft.public.dotnet.languages.vc)

Loading