/// <summary> /// Schedules the specified object to be released when the caller thread terminates. Note that this operation /// is intended to simplify reference counting of ephemeral objects during unit tests. Do not use it beyond the /// intended use case. /// </summary> public static T ReleaseLater <T>(T msg, int decrement) { if (msg is IReferenceCounted referenceCounted) { ThreadDeathWatcher.Watch(Thread.CurrentThread, () => { try { if (!referenceCounted.Release(decrement)) { Logger.NonZeroRefCnt(referenceCounted, decrement); } #if DEBUG else { if (Logger.DebugEnabled) { Logger.ReleasedObject(referenceCounted, decrement); } } #endif } catch (Exception ex) { Logger.FailedToReleaseAObject(referenceCounted, ex); } }); } return(msg); }