protected RefCountedMemoryLifetimeGuard()
        {
            if (MemoryDiagnostics.UndisposedAllocationSubscribed)
            {
                this.allocationStackTrace = Environment.StackTrace;
            }

            MemoryDiagnostics.IncrementTotalUndisposedAllocationCount();
        }
        private void ReleaseRef(bool finalizing)
        {
            Interlocked.Decrement(ref this.refCount);
            if (this.refCount == 0)
            {
                int wasReleased = Interlocked.Exchange(ref this.released, 1);

                if (wasReleased == 0)
                {
                    if (!finalizing)
                    {
                        MemoryDiagnostics.DecrementTotalUndisposedAllocationCount();
                    }
                    else if (this.allocationStackTrace != null)
                    {
                        MemoryDiagnostics.RaiseUndisposedMemoryResource(this.allocationStackTrace);
                    }

                    this.Release();
                }
            }
        }