diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
index f01b9697..d92681b8 100644
--- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
+++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs
@@ -1552,6 +1552,15 @@ null//comment
]/*comment*/
}/*comment *//*comment 1 */", sw.ToString());
}
+
+ [Test]
+ public void DisposeSupressesFinalization()
+ {
+ UnmanagedResourceFakingJsonWriter.CreateAndDispose();
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ Assert.AreEqual(1, UnmanagedResourceFakingJsonWriter.DisposalCalls);
+ }
}
public class CustomJsonTextWriter : JsonTextWriter
@@ -1720,4 +1729,35 @@ null//comment
}
}
#endif
+
+ public class UnmanagedResourceFakingJsonWriter : JsonWriter
+ {
+ public static int DisposalCalls;
+
+ public static void CreateAndDispose()
+ {
+ ((IDisposable)new UnmanagedResourceFakingJsonWriter()).Dispose();
+ }
+
+ public UnmanagedResourceFakingJsonWriter()
+ {
+ DisposalCalls = 0;
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ base.Dispose(disposing);
+ ++DisposalCalls;
+ }
+
+ ~UnmanagedResourceFakingJsonWriter()
+ {
+ Dispose(false);
+ }
+
+ public override void Flush()
+ {
+ throw new NotImplementedException();
+ }
+ }
}
\ No newline at end of file
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs
index 67cac53a..a6bc5342 100644
--- a/Src/Newtonsoft.Json/JsonWriter.cs
+++ b/Src/Newtonsoft.Json/JsonWriter.cs
@@ -1437,9 +1437,14 @@ namespace Newtonsoft.Json
void IDisposable.Dispose()
{
Dispose(true);
+ GC.SuppressFinalize(this);
}
- private void Dispose(bool disposing)
+ ///
+ /// Releases unmanaged and - optionally - managed resources
+ ///
+ /// true to release both managed and unmanaged resources; false to release only unmanaged resources.
+ protected virtual void Dispose(bool disposing)
{
if (_currentState != State.Closed)
{