6

Possible Duplicate:
Proper use of the IDisposable interface

"IDisposable Interface" article tells:

The primary use of this interface is to release unmanaged resources

Why? Why only unmanaged?

Whole my life I thought its PRIMIRALY use is to release ANY resources: managed (connections to DBs, services proxies, etc) and unmanaged (if they are used in application).

P.S.

I believe there are already questions on this topic, but can't find them.

Community
  • 1
  • 1
Budda
  • 18,015
  • 33
  • 124
  • 206
  • 1
    Managed resources are cleaned up automatically by the garbage collector. IDiposable provides a standard way for unmanaged resources to be released. – Matt Davis Jun 23 '11 at 02:11
  • Try here -- lots of good info: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface – Jon Seigel Jun 23 '11 at 02:11

5 Answers5

6

The underlying connections to db's are not managed, as are file handles and a number of other low-level o/s objects. They are unmanaged. Implementing an IDisposable interface implies that you are not just relying on the garbage collector to release those resources; but you are closing those resources using what ever low-level API that you have available.

Also, I think Eric Lippert's answer (2nd one down) to a similar question is a very good explanation on why you would use IDisposable.

Community
  • 1
  • 1
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • Nevertheless question is closed as duplicated, I want to accept this answer. David, you are the only person who took my attention that connection to DB is actually unmanaged resource... It makes sense. I knew that I always need to close it (or dispose), but I always thought they are named as 'managed'. Thanks! – Budda Jul 02 '11 at 04:55
5

If you read further there is an explanation:

The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Garbage collector takes care about managed resources. This is why they are managed.

Also, connection resource in your example is not managed resource. .NET connection classes wrap unmanaged resources.

Alex Aza
  • 76,499
  • 26
  • 155
  • 134
2

IDisposable.Dispose() is responsible for two things:

  • Releasing unmanaged resources that the object might own
  • Dispose()ing other IDisposables owned by the object
TrueWill
  • 25,132
  • 10
  • 101
  • 150
user541686
  • 205,094
  • 128
  • 528
  • 886
0

Your answer to

Why? Why only unmanaged?

Lifetime of the managed resources are controlled by garbage collector. Which is one of the nice reason that you use C# or Java.

crypted
  • 10,118
  • 3
  • 39
  • 52
0

Instead of "unmanaged resources", think "responsibilities". When an object is described as holding "unamanged resources", what that really means is that:

  1. The class has the information and impetus necessary to do something to an outside entity.
  2. If that action never gets done, something else won't work as well as it otherwise would (the effects may be minor or severe).
  3. If the class is doesn't perform the action, nothing else will.

The most common situation where a class will have cleanup responsibilities is when some other entity has been asked to reserve something (be it a file, GDI handle, lock, array slot, memory block, communications channel, or whatever) until further notice. If nothing tells that other entity that the thing it's reserved is no longer needed, it will never allow anything else to use it.

If an object which has an important responsibility to perform some action gets swept away by the garbage collector before fulfilling its responsibility, the action will never get performed. There are two ways this can be prevented:

  1. If an object implements IDisposable, "someone" (either another object or a running procedure) should be slated to call Dispose method before it's abandoned. Dispose shouldn't be thought of as destroying an object, but rather telling an object to carry out its final responsibilities so it may be safely abandoned.
  2. Objects can ask the system to let them know when they've been abandoned, before they're swept away. While such notifications can reduce the danger that a required action might never be performed, it is dangerous to rely upon them since they will often not come in a particularly timely fashion, and in some cases may never come at all.

Objects which provide for the second cleanup approach are called "managed resources".

supercat
  • 77,689
  • 9
  • 166
  • 211