Skip to content

Instantly share code, notes, and snippets.

@chrisnas
Last active January 12, 2019 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnas/38a2f2e860632cdac4f9a32fad71f689 to your computer and use it in GitHub Desktop.
Save chrisnas/38a2f2e860632cdac4f9a32fad71f689 to your computer and use it in GitHub Desktop.
/// 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