Correctly implemented IDisposable

Correctly implemented IDisposable on the client's and server's
ProtoWriter.
This commit is contained in:
yankejustin 2015-03-17 13:28:23 -04:00
parent 9a10e2f992
commit ba2f2a0b2a
2 changed files with 82 additions and 13 deletions

View File

@ -471,18 +471,31 @@ public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
/// </summary>
public SerializationContext Context { get { return context; } }
/// <summary>
/// Disposes and cleans up all unused resources that is used by the object.
/// </summary>
void IDisposable.Dispose()
{
Dispose(false);
}
void Dispose()
{
// Dispose of it all.
Dispose(true);
// Don't waste time finalizing because we have already disposed of the object ourselves.
GC.SuppressFinalize(this);
}
/// <summary>
/// Release some resources of the ProtoWriter, but NOT the underlying stream.
/// </summary>
public void Dispose()
{
// ProtoWriter's own definition of dispose. Will release some resources, but not the underlying stream.
Dispose(false);
}
/// <summary>
/// Releases some resources of the ProtoWriter. Optionally dispose of it all, including the underlying stream.
/// </summary>
/// <param name="disposing">Determines whether to dispose of everything, including the underlying stream.</param>
public void Dispose(bool disposing)
{
if (!disposed)
@ -512,6 +525,14 @@ public void Dispose(bool disposing)
}
}
~ProtoWriter()
{
// The object has been destroyed. Make sure everything is cleaned up.
// If the object has already been fully disposed, this finalizer would
// be suppressed.
Dispose(true);
}
private byte[] ioBuffer;
private int ioIndex;
// note that this is used by some of the unit tests and should not be removed

View File

@ -23,6 +23,8 @@ public sealed class ProtoWriter : IDisposable
{
private Stream dest;
TypeModel model;
private bool disposed = false;
/// <summary>
/// Write an encapsulated sub-object, using the supplied unique key (reprasenting a type).
/// </summary>
@ -467,20 +469,66 @@ public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
/// Addition information about this serialization operation.
/// </summary>
public SerializationContext Context { get { return context; } }
/// <summary>
/// Disposes and cleans up all unused resources that is used by the object.
/// </summary>
void IDisposable.Dispose()
{
Dispose();
// Dispose of it all.
Dispose(true);
// Don't waste time finalizing because we have already disposed of the object ourselves.
GC.SuppressFinalize(this);
}
private void Dispose()
/// <summary>
/// Release some resources of the ProtoWriter, but NOT the underlying stream.
/// </summary>
public void Dispose()
{
if (dest != null)
// ProtoWriter's own definition of dispose. Will release some resources, but not the underlying stream.
Dispose(false);
}
/// <summary>
/// Releases some resources of the ProtoWriter. Optionally dispose of it all, including the underlying stream.
/// </summary>
/// <param name="disposing">Determines whether to dispose of everything, including the underlying stream.</param>
public void Dispose(bool disposing)
{
if (!disposed)
{
Flush(this);
dest.Dispose();
dest = null;
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
if (disposing)
{
if (dest != null)
{
try
{
// Flush can throw a few exceptions, so make sure that, even if it messes up, dispose of the underlying stream.
Flush(this);
}
finally
{
dest.Dispose();
dest = null;
}
}
disposed = true;
}
}
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
}
~ProtoWriter()
{
// The object has been destroyed. Make sure everything is cleaned up.
// If the object has already been fully disposed, this finalizer would
// be suppressed.
Dispose(true);
}
private byte[] ioBuffer;