private static void UnregisterInstance(Type key, object value) { //Minimize contention by seperately locking tracked instances and the internal list Dictionary <int, TrackedDisposable> undisposedInstances = null; lock (_TrackedInstances) { _TrackedInstances.TryGetValue(key, out undisposedInstances); } if (undisposedInstances == null) { return; } var hash = System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(value); TrackedDisposable trackedItem = null; lock (undisposedInstances) { if (undisposedInstances.TryGetValue(hash, out trackedItem)) { undisposedInstances.Remove(hash); } } if (trackedItem != null) { DisposableUnregisteredLogger?.Invoke(trackedItem); } }
private static void RegisterInstance(Type key, IDisposable value) { if (!Enabled) { return; } lock (_TrackedTypes) { if (_TrackedTypes.Count > 0 && !_TrackedTypes.ContainsKey(key)) { return; } } //Generate these outside of lock to minimize contention var trackedDisposable = new TrackedDisposable(value, key) { CreationStackTrace = (CaptureStackTraceAtCreation ? GetCreationStackTrace() : null) }; //Minimize contention by seperately locking tracked instances and the internal list Dictionary <int, TrackedDisposable> undisposedInstances = null; lock (_TrackedInstances) { if (!_TrackedInstances.TryGetValue(key, out undisposedInstances)) { undisposedInstances = new Dictionary <int, TrackedDisposable>(); _TrackedInstances.Add(key, undisposedInstances); } } var hash = System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(value); lock (undisposedInstances) { undisposedInstances[hash] = trackedDisposable; } DisposableRegisteredLogger?.Invoke(trackedDisposable); }