Many types of unmanaged resources can satisfy their cleanup responsibilities by using a simple API call on the handle. Such resources can (and often should) be useful encapsulated in SafeHandle. Some other types of unmanaged resources (for example, subscriptions to events held by long-lived publishers) have cleaning responsibilities that cannot be handled with a finalizer. Finalizers can be useless if semantically useless links refer to objects left alive and may be unsuitable for resources that need to be cleared by the thread that creates them. There is no need to write a custom finalizer for such resources, because no type of finalizer will eliminate the absolute 100% need to provide a deterministic solution.
However, some types of resources may benefit from having a finalizer, even if they need to perform actions that are not allowed within the scope of the constraints. Finalizers can be useful in situations where the background thread is responsible for manipulating the object and where the main application stores the object, which, in turn, contains a link to the real object. The finalizer of the main application link binding object may signal a background thread that the object that it supports is no longer needed. Such an alarm must be done carefully to ensure that it does not break the finalizer threads, but if done with caution, it may be useful to have it in the finalizer, even if this is not permitted in CER.
source share