How to properly dispose collection of unmanaged resources from finalizer?

Lea Hayes

Here is an example about which I am uncertain:

public class SomeClass : IDisposable {

    ~SomeClass() {
        Dispose(false);
    }

    public void Dispose() {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private bool _disposed;

    protected virtual void Dispose(bool disposing) {
        if (!_disposed) {
            if (disposing) {
                // TODO: Release any managed resources here...
            }

            // ?! Is it safe to enumerate the dictionary here ?!
            foreach (var resource in _resources.Values)
                ReleaseBuffer(resource);
            _resources = null;

            _disposed = true;
        }
    }

   private Dictionary<string, IntPtr> _resources;

    ...

}

Will it be safe to enumerate the managed dictionary in order to release the unmanaged resources?

Is availability of the dictionary uncertain since the order in which finalizers are invoked is not defined?

Here is a quote taken from the MSDN which I find confusing [1]:

  • The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already been finalized when the finalizer of Object A starts.
  1. http://msdn.microsoft.com/en-us/library/system.object.finalize(v=vs.110).aspx
supercat

Rather than having a dictionary of unmanaged resources, I would suggest having a dictionary of independent wrapper objects, each of which is responsible for guarding one unmanaged resource. If the object holding the dictionary is abandoned and no other references exist to the wrapper objects, all of the wrapper objects will get finalized without needing to involve the dictionary itself in the process. Using such an approach will make it easier to sanely handle cases in which an exception occurs during object construction, and at least somewhat-sanely deal with situations where an object finds itself resurrected between the time it has been enqueued for finalization and the time the finalizer runs [code generally can't be expected to run "correctly" in such cases, but should avoid corrupting the state of the rest of the system].

For example, code which uses a handle may acquire a lock during its use and, after use, check a "disposeObjectASAP" flag; if set, re-acquire the lock and dispose object. The finalizer itself should set the flag and then try to acquire the lock; if it successfully acquires the lock, it should dispose the object. If unable, the fact that it set the flag should imply that code which has the lock is destined to check the flag and clean up the object, so the finalizer doesn't have to. If the finalizer runs prematurely, it may release resources which another thread is going to need, causing actions on that other thread to fail, but the finalizer won't release resources while another thread is using them or disposing them, since releasing resources in those situations could cause massive system corruption.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to properly dispose collection of unmanaged resources from finalizer?

From Dev

How to properly dispose of pinvoke/unmanaged code

From Dev

How to properly dispose of pinvoke/unmanaged code

From Dev

Why should Dispose() dispose managed resources and finalizer not?

From Dev

How to dispose unmanaged objects from static class in c#?

From Dev

Why NamedScopes invoke dispose methods from finalizer?

From Dev

How to dispose/release/"finalize" unmanaged resources when a shared value gets out of scope

From Dev

How to Dispose resources in this condition

From Dev

How to dispose managed and unmanaged objects in C#?

From Dev

How to use dispose() properly?

From Dev

How to properly dispose Scene3D from QML?

From Dev

How to properly dispose of ThreadLocal variables?

From Dev

How to properly dispose of a pthread mutex?

From Dev

How to properly dispose objects in Flambe?

From Dev

Calling .Dispose() on a class that has a Finalizer

From Dev

How to Dispose resources when the WPF form close

From Dev

How to Dispose resources when the WPF form close

From Dev

How to properly use the dispose method on a class

From Dev

How to dispose properly using async and await

From Dev

How to properly dispose the stream when using StreamContent

From Dev

How to properly use the dispose method on a class

From Dev

How to close or dispose of TCP client properly?

From Dev

Overhead of having a Finalizer - with/without SuppressFinalize in Dispose

From Dev

Throwing exception in finalizer to enforce Dispose calls:

From Dev

Disposing unmanaged resources in a form

From Dev

Disposing unmanaged resources in a form

From Dev

C - properly importing stdcall functions from unmanaged DLL

From Dev

How to properly marshal unmanaged arrays in c# without unsafe

From Dev

How to properly marshal unmanaged arrays in c# without unsafe

Related Related

  1. 1

    How to properly dispose collection of unmanaged resources from finalizer?

  2. 2

    How to properly dispose of pinvoke/unmanaged code

  3. 3

    How to properly dispose of pinvoke/unmanaged code

  4. 4

    Why should Dispose() dispose managed resources and finalizer not?

  5. 5

    How to dispose unmanaged objects from static class in c#?

  6. 6

    Why NamedScopes invoke dispose methods from finalizer?

  7. 7

    How to dispose/release/"finalize" unmanaged resources when a shared value gets out of scope

  8. 8

    How to Dispose resources in this condition

  9. 9

    How to dispose managed and unmanaged objects in C#?

  10. 10

    How to use dispose() properly?

  11. 11

    How to properly dispose Scene3D from QML?

  12. 12

    How to properly dispose of ThreadLocal variables?

  13. 13

    How to properly dispose of a pthread mutex?

  14. 14

    How to properly dispose objects in Flambe?

  15. 15

    Calling .Dispose() on a class that has a Finalizer

  16. 16

    How to Dispose resources when the WPF form close

  17. 17

    How to Dispose resources when the WPF form close

  18. 18

    How to properly use the dispose method on a class

  19. 19

    How to dispose properly using async and await

  20. 20

    How to properly dispose the stream when using StreamContent

  21. 21

    How to properly use the dispose method on a class

  22. 22

    How to close or dispose of TCP client properly?

  23. 23

    Overhead of having a Finalizer - with/without SuppressFinalize in Dispose

  24. 24

    Throwing exception in finalizer to enforce Dispose calls:

  25. 25

    Disposing unmanaged resources in a form

  26. 26

    Disposing unmanaged resources in a form

  27. 27

    C - properly importing stdcall functions from unmanaged DLL

  28. 28

    How to properly marshal unmanaged arrays in c# without unsafe

  29. 29

    How to properly marshal unmanaged arrays in c# without unsafe

HotTag

Archive