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> /// </summary>
void IDisposable.Dispose() void IDisposable.Dispose()
{ {
// Dispose of it all. Dispose();
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> /// <summary>
/// Releases some resources of the ProtoWriter. Optionally dispose of it all, including the underlying stream. /// Releases some resources of the ProtoWriter. Optionally dispose of it all, including the underlying stream.
/// </summary> /// </summary>
/// <param name="disposing">Determines whether to dispose of everything, including the underlying stream.</param> public void Dispose()
public void Dispose(bool disposing)
{
if (!disposed)
{
model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
if (disposing)
{ {
if (dest != null) 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); Flush(this);
}
finally
{
dest.Dispose(); dest.Dispose();
dest = null; dest = null;
} }
} model = null;
BufferPool.ReleaseBufferToPool(ref ioBuffer);
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);
} }
private byte[] ioBuffer; private byte[] ioBuffer;

View File

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