protected override void Dispose(bool disposing)
{
if (this.disposed)
{
string doubleDisposeStack = null;
if (this.memoryManager.GenerateCallStacks)
{
doubleDisposeStack = PclExport.Instance.GetStackTrace();
}
Events.Write.MemoryStreamDoubleDispose(this.id, this.tag, this.allocationStack, this.disposeStack, doubleDisposeStack);
return;
}
Events.Write.MemoryStreamDisposed(this.id, this.tag);
if (this.memoryManager.GenerateCallStacks)
{
this.disposeStack = PclExport.Instance.GetStackTrace();
}
if (disposing)
{
// Once this flag is set, we can't access any properties -- use fields directly
this.disposed = true;
this.memoryManager.ReportStreamDisposed();
GC.SuppressFinalize(this);
}
else
{
// We're being finalized.
Events.Write.MemoryStreamFinalized(this.id, this.tag, this.allocationStack);
#if !(PCL || NETSTANDARD1_1)
if (AppDomain.CurrentDomain.IsFinalizingForUnload())
{
// If we're being finalized because of a shutdown, don't go any further.
// We have no idea what's already been cleaned up. Triggering events may cause
// a crash.
base.Dispose(disposing);
return;
}
#endif
this.memoryManager.ReportStreamFinalized();
}
this.memoryManager.ReportStreamLength(this.length);
if (this.largeBuffer != null)
{
this.memoryManager.ReturnLargeBuffer(this.largeBuffer, this.tag);
}
if (this.dirtyBuffers != null)
{
foreach (var buffer in this.dirtyBuffers)
{
this.memoryManager.ReturnLargeBuffer(buffer, this.tag);
}
}
this.memoryManager.ReturnBlocks(this.blocks, this.tag);
base.Dispose(disposing);
}