Last active
January 12, 2019 21:14
-
-
Save chrisnas/38a2f2e860632cdac4f9a32fad71f689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// T is the type to finalize | |
/// S contains the native state of T to be cleaned up | |
/// This separation enforces the pattern where the "native" part stored by a managed type is kept in a State | |
/// The state could even be used as is in T (kind of a SafeHandle holding several native resources) | |
public class Cleaner<T, S> | |
where T : class | |
{ | |
... | |
private readonly ReferenceQueue<T> _queue; | |
public Cleaner(Action<S> onCleanup, Action<Exception> onError) | |
{ | |
_onCleanup = onCleanup; | |
_onError = onError; | |
_queue = new ReferenceQueue<T>(); | |
... | |
} | |
private Action<S> _onCleanup; | |
private Action<Exception> _onError; | |
... | |
public void Track(T value, S state) | |
{ | |
var phantomReference = new PhantomObjectFinalizer<T, S>(_queue, value, state); | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment