Fixed ProtoWriter Dispose

this broke protobuf
This commit is contained in:
MaxXor 2015-03-17 22:28:03 +01:00
parent b45ebd33ae
commit 84b7fc901e
2 changed files with 50 additions and 123 deletions

View File

@ -474,60 +474,22 @@ public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
/// </summary>
void IDisposable.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);
Dispose();
}
/// <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)
{
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
if (disposing)
public void Dispose()
{
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;
}
}
}
~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);
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
}
private byte[] ioBuffer;

View File

@ -136,7 +136,8 @@ internal NetObjectCache NetCache
/// <summary>
/// Writes a field-header, indicating the format of the next data we plan to write.
/// </summary>
public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer) {
public static void WriteFieldHeader(int fieldNumber, WireType wireType, ProtoWriter writer)
{
if (writer == null) throw new ArgumentNullException("writer");
if (writer.wireType != WireType.None) throw new InvalidOperationException("Cannot write a " + wireType.ToString()
+ " header until the " + writer.wireType.ToString() + " data has been written");
@ -157,7 +158,8 @@ internal NetObjectCache NetCache
throw new ArgumentException("Invalid wire-type: " + wireType.ToString(), "wireType");
}
#endif
if (writer.packedFieldNumber == 0) {
if (writer.packedFieldNumber == 0)
{
writer.fieldNumber = fieldNumber;
writer.wireType = wireType;
WriteHeaderCore(fieldNumber, wireType, writer);
@ -474,60 +476,22 @@ public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
/// </summary>
void IDisposable.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);
Dispose();
}
/// <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)
{
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
if (disposing)
public void Dispose()
{
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;
}
}
}
~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);
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
}
private byte[] ioBuffer;
@ -591,7 +555,8 @@ private static void WriteUInt32Variant(uint value, ProtoWriter writer)
{
DemandSpace(5, writer);
int count = 0;
do {
do
{
writer.ioBuffer[writer.ioIndex++] = (byte)((value & 0x7F) | 0x80);
count++;
} while ((value >>= 7) != 0);