What is the difference between Managed and Unmanaged resources in regards to IDisposable

D

DaTurk

Hi,
I'm implementing the Idisposable pattern and am wondering what the
difference it between managed, and unmanaged resources?

http://msdn2.microsoft.com/en-us/library/fs2xkftw.aspx

in the code snippet where it's overloading the Dispose method it
disposes of managed and unmanaged resources if disposing == true, and
only unmanaged resources if disposing is false. I'm not sure I
completely understand. Thank you in advance.
 
D

DaTurk

Is a managed resource an object that has it's own dispose method, and
an unmanged resource one that you have to clean up, for example a
socket would be an unmanged resource as even though it's an object, it
has no dispose method, so you have to explicitly shutdown the socket,
close the socket and then null it out. Is this right?
 
B

Brian Gideon

Examples of unmanaged resources include operating system handles, file
handles, memory allocated by a direct call to a Win32 API, etc.
Unmanaged resources usually come into play when you use the interop
features provided by the framework.

The pattern dictates that disposing == true when called from the public
Dispose method. That means you (the developer) have chosen to
deterministicly dispose the object either directly by calling Dispose
or indirectly by using on of the C# constructs that automatically
generates the call to Dispose. If disposing == false then that means
the object is being disposed by the GC through the Finalize
(destructor) method.

The reason the pattern has two separate branches is because managed
resources cannot be touched when disposing == false since the GC may
have already disposed them.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top