Re: " //Clean Up managed resources " f&*ck
- From: Jesse Houwing <jesse.houwing@xxxxxxxxxxxxxxxx>
- Date: Sat, 19 May 2007 16:15:13 +0200
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
.
- Follow-Ups:
- Re: " //Clean Up managed resources " f&*ck
- From: Koliber (js)
- Re: " //Clean Up managed resources " f&*ck
- 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)
- " //Clean Up managed resources " f&*ck
- Prev by Date: Re: Event Handlers
- Next by Date: Re: How to obtain template for .Net 3.0 with vs2005 ?
- Previous by thread: Re: " //Clean Up managed resources " f&*ck
- Next by thread: Re: " //Clean Up managed resources " f&*ck
- Index(es):
Relevant Pages
|
Loading