Basic use of the IDisposable interface

Possible duplicate:
Proper use of the IDisposable interface

An article with IDisposable Interface reports:

The main use of this interface is the release of unmanaged resources

Why? Why only uncontrollable?

Throughout my life, I thought that PRIMIRALY uses it to free ANY resources: managed (database connections, service proxies, etc.) and unmanaged (if they are used in the application).

PS

I believe that there are already questions on this topic, but they cannot be found.

+6
source share
5 answers

Suitable db connections are not controlled, as are file descriptors and a number of other low-level o / s objects. They are uncontrollable. Implementing the IDisposable interface implies that you do not just rely on the garbage collector to free up these resources; but you close these resources using some low-level API that you have.

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

+6
source

If you read further, there is an explanation:

The garbage collector automatically frees the memory allocated to the managed object if this object is absent longer used. However, this cannot be predicted when garbage collection will occur. Moreover, the garbage collector does not know unmanaged resources, such as window handles or open files and streams.

The garbage collector takes care of managed resources. That is why they are managed .

In addition, the connection resource in your example is not managed by the resource. The classes of .NET connections carry unmanaged resources.

+5
source

IDisposable.Dispose() is responsible for two things:

  • Release unmanaged resources an object can own
  • Dispose() ing another IDisposable owned by the object
+2
source

Your answer to

Why? Why only uncontrollable?

Managed Resource Life is controlled by the garbage collector. This is one of the nice reasons to use C # or Java.

0
source

Instead of “unmanaged resources,” think “responsibilities.” When an object is described as containing "unchanged resources", this means that:

  • The class has the information and incentive necessary to do something for an external object.
  • If this action is never performed, something else will not work as well as otherwise (the effects may be minor or serious).
  • If the class does not perform the action, there will be nothing more.

The most common situation where a class will have responsibilities for cleaning up is when some other object is asked to reserve something (be it a file, a GDI descriptor, a lock, a memory slot, a memory block, a communication channel or something else), bye notice. If nothing says that the other entity, that the reserved thing is no longer needed, it will never allow it to use anything else.

If an object that has important responsibility for performing an action is swept away by the garbage collector before fulfilling its responsibility, the action will never be performed. There are two ways to prevent this:

  1. If an object implements IDisposable, "someone" (either another object or a running procedure) must be assigned to call the Dispose method before it fails. Disposal cannot be regarded as the destruction of an object, but rather to inform the object of the fulfillment of its final duties so that it can be safely left.
  2. Objects may ask the system to tell them when they were left before they are swept away. Although such notifications can reduce the risk that the required action will never be completed, it is dangerous to rely on them, as they often do not arrive especially timely, and in some cases may not appear at all.

    Objects that provide a second approach to cleanup are called "managed resources."

0
source

Source: https://habr.com/ru/post/891173/


All Articles