From c3368aab65f11a59ab4c0813e8a0d7987a6446bd Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 26 Aug 2022 12:06:30 -0700 Subject: [PATCH] Fix ToString to correctly handle disposed streams. (#244) * Fix ToString to correctly handle disposed strings. * Resolve PR comment Co-authored-by: Ben Watson --- src/RecyclableMemoryStream.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/RecyclableMemoryStream.cs b/src/RecyclableMemoryStream.cs index 8be277e5..786c7f61 100644 --- a/src/RecyclableMemoryStream.cs +++ b/src/RecyclableMemoryStream.cs @@ -1087,7 +1087,15 @@ public override void Write(ReadOnlySpan source) /// public override string ToString() { - return $"Id = {this.Id}, Tag = {this.Tag}, Length = {this.Length:N0} bytes"; + if (!this.disposed) + { + return $"Id = {this.Id}, Tag = {this.Tag}, Length = {this.Length:N0} bytes"; + } + else + { + // Avoid properties because of the dispose check, but the fields themselves are not cleared. + return $"Disposed: Id = {this.id}, Tag = {this.tag}, Final Length: {this.length:N0} bytes"; + } } ///