Unmanaged resources and Dispose()

14,304

Solution 1

In this context it's perhaps easiest to think of it like this:

  • An unmanaged resource is any resource that you obtained by making a Windows API call that returns a Windows Handle that must be freed at some point.

  • The only other kind of resource is memory. This is managed automatically if it was allocated by .Net. (Note that there are ways to allocate memory using the Windows API; this counts as an unmanaged resource.)

For example, the FileStream class calls the Windows API to open a file, for which FileStream keeps a file handle internally. That file handle represents an unmanaged resource that must be freed at some point.

FileStream uses the Windows API function CreateFile() behind the scenes. It is the handle returned from CreateFile which represents an unmanaged resource.

If you don't free those handles, they will remain allocated for the duration of the program, but all .Net classes that have an unmanaged resource provide a Finalizer (see below) to make sure that they will normally be freed at some point.

(But if you were writing your own file handling class and forgot to free the file handle anywhere at all, the file would remain open until your program exited.)

Normally such unmanaged resources will be freed in two places:

  • The Dispose() method. This should be the normal way that you dispose unmanaged resources.

  • The Finalizer. This is a last-resort mechanism. If a class has a finalizer it will be called by the Garbage Collector when it cleans up a dead object. Any class which has an unmanaged resource should have a finalizer to clean up if the programmer forgets to call Dispose().

This is somewhat of a simplification, but it will help you understand it I hope.

For full details, see this MSDN article on the Dispose Pattern.

Solution 2

Some people think of unmanaged resources as being related to Windows API calls or other such things, but that's an implementation detail. The fundamental characteristic of an unmanaged resource is that it represents an aspect of an outside entity's state which is being maintained for the benefit of the object owning the resource, to the potential detriment of other entities, and which the outside entity would continue to maintain--uselessly--if all references to the owner were abandoned. Note that the outside entity could be anything, anywhere. Windows API handles represent a tiny subset of the possibilities (an object that acquires a handle asks Windows to make some aspects of the system available for its exclusive use, to the detriment of any other code that would want to use them). It's entirely possible that the outside entity is within the same assembly (especially if the resource is a lock or an event subscription), or that it might be on another continent (if the resource is a file on a remote machine).

An object releases a resource by notifying entities that are acting on its behalf that they no longer need to do so. A managed resource is a .NET heap object which may own some combination of managed or unmanaged resources, but whose resources will likely get released if it is abandoned.

Share:
14,304
Aparan
Author by

Aparan

Updated on August 25, 2022

Comments

  • Aparan
    Aparan over 1 year

    I was reading some articles about Dispose() method and found that unmanaged resources should be freed explicitly from Dispose() method (or finalize() method) and the article says file handles and database connection objects are examples of unmanaged resources. Can anyone explain why those are unmanaged and what happens if not handled properly in Dispose()? I have no idea about what a file handle is. Where do they exist?