diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index 53cc5680..56d061e2 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -3520,6 +3520,77 @@ null//comment Console.WriteLine("Done"); } #endif + [Test] + public void DisposeSupressesFinalization() + { + UnmanagedResourceFakingJsonReader.CreateAndDispose(); + GC.Collect(); + GC.WaitForPendingFinalizers(); + Assert.AreEqual(1, UnmanagedResourceFakingJsonReader.DisposalCalls); + } + } + + public class UnmanagedResourceFakingJsonReader : JsonReader + { + public static int DisposalCalls; + + public static void CreateAndDispose() + { + ((IDisposable)new UnmanagedResourceFakingJsonReader()).Dispose(); + } + + public UnmanagedResourceFakingJsonReader() + { + DisposalCalls = 0; + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + ++DisposalCalls; + } + + ~UnmanagedResourceFakingJsonReader() + { + Dispose(false); + } + + public override bool Read() + { + throw new NotImplementedException(); + } + + public override byte[] ReadAsBytes() + { + throw new NotImplementedException(); + } + + public override DateTime? ReadAsDateTime() + { + throw new NotImplementedException(); + } + +#if !NET20 + public override DateTimeOffset? ReadAsDateTimeOffset() + { + throw new NotImplementedException(); + } +#endif + + public override decimal? ReadAsDecimal() + { + throw new NotImplementedException(); + } + + public override int? ReadAsInt32() + { + throw new NotImplementedException(); + } + + public override string ReadAsString() + { + throw new NotImplementedException(); + } } public class ToggleReaderError : TextReader diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index f9109ce8..961dbb8d 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -1085,6 +1085,7 @@ namespace Newtonsoft.Json void IDisposable.Dispose() { Dispose(true); + GC.SuppressFinalize(this); } ///