Suppress finaliser in Dispose if there's a protected Dispose(bool)

Fixes #746
This commit is contained in:
Jon Hanna
2015-12-18 19:03:36 +00:00
parent 48786adc5b
commit 40bd051809
2 changed files with 72 additions and 0 deletions
@@ -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
+1
View File
@@ -1085,6 +1085,7 @@ namespace Newtonsoft.Json
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>