From 004f2054128e000a8ee4a327b4f429e10e4275b8 Mon Sep 17 00:00:00 2001 From: MaxPatri Date: Sun, 22 Dec 2024 10:34:30 +0300 Subject: [PATCH] Fix ReadBufferState dispose cases (#110739) * Move ReadBufferState initialization after other operations * Move ReadBufferState initialization after other operations * Update src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs * Move ReadBufferState initialization under try clause * Move ReadBufferState initialization under try clause * Return initial change. Add explanatory comment * Return initial change. Add explanatory comment * Fix typo * Fix typo * Update comment * Update comment * Change bufferState initialization style --------- Co-authored-by: Eirik Tsarpalis --- .../Text/Json/Serialization/JsonSerializer.Read.Stream.cs | 3 ++- .../Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs index 75328f71473c36..d804cca460c2bd 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.Stream.cs @@ -553,10 +553,11 @@ public static partial class JsonSerializer { Debug.Assert(listTypeInfo.IsConfigured); - ReadBufferState bufferState = new(listTypeInfo.Options.DefaultBufferSize); ReadStack readStack = default; readStack.Initialize(listTypeInfo, supportContinuation: true); JsonReaderState jsonReaderState = new(readerOptions); + // Note: The ReadBufferState ctor rents pooled buffers. + ReadBufferState bufferState = new(listTypeInfo.Options.DefaultBufferSize); try { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs index bfe0f394d42717..3ea5e94b8ce9cb 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonTypeInfoOfT.ReadHelper.cs @@ -26,10 +26,11 @@ public partial class JsonTypeInfo { Debug.Assert(IsConfigured); JsonSerializerOptions options = Options; - var bufferState = new ReadBufferState(options.DefaultBufferSize); ReadStack readStack = default; readStack.Initialize(this, supportContinuation: true); var jsonReaderState = new JsonReaderState(options.GetReaderOptions()); + // Note: The ReadBufferState ctor rents pooled buffers. + ReadBufferState bufferState = new(options.DefaultBufferSize); try { @@ -58,10 +59,11 @@ public partial class JsonTypeInfo { Debug.Assert(IsConfigured); JsonSerializerOptions options = Options; - var bufferState = new ReadBufferState(options.DefaultBufferSize); ReadStack readStack = default; readStack.Initialize(this, supportContinuation: true); var jsonReaderState = new JsonReaderState(options.GetReaderOptions()); + // Note: The ReadBufferState ctor rents pooled buffers. + ReadBufferState bufferState = new(options.DefaultBufferSize); try {