Dispose of MemoryStream

IExtension.EndAppend is responsible for cleaning up stream (as it
stated). However, this method created a new memory stream from the
stream sent to the method that was not cleaned up.
This commit is contained in:
yankejustin 2015-03-17 12:30:59 -04:00
parent 2fd2001057
commit be233599db
1 changed files with 20 additions and 15 deletions

View File

@ -27,18 +27,18 @@ void IExtension.EndAppend(Stream stream, bool commit)
int len; int len;
if (commit && (len = (int)stream.Length) > 0) if (commit && (len = (int)stream.Length) > 0)
{ {
MemoryStream ms = (MemoryStream)stream; using (MemoryStream ms = (MemoryStream)stream)
{
if (buffer == null) if (buffer == null)
{ // allocate new buffer { // allocate new buffer
buffer = ms.ToArray(); buffer = ms.ToArray();
} }
else else
{ // resize and copy the data { // resize and copy the data
// note: Array.Resize not available on CF // note: Array.Resize not available on CF
int offset = buffer.Length; int offset = buffer.Length;
byte[] tmp = new byte[offset + len]; byte[] tmp = new byte[offset + len];
Helpers.BlockCopy(buffer, 0, tmp, 0, offset); Helpers.BlockCopy(buffer, 0, tmp, 0, offset);
#if PORTABLE || WINRT // no GetBuffer() - fine, we'll use Read instead #if PORTABLE || WINRT // no GetBuffer() - fine, we'll use Read instead
int bytesRead; int bytesRead;
@ -52,9 +52,10 @@ void IExtension.EndAppend(Stream stream, bool commit)
if(len != 0) throw new EndOfStreamException(); if(len != 0) throw new EndOfStreamException();
ms.Position = oldPos; ms.Position = oldPos;
#else #else
Helpers.BlockCopy(ms.GetBuffer(), 0, tmp, offset, len); Helpers.BlockCopy(ms.GetBuffer(), 0, tmp, offset, len);
#endif #endif
buffer = tmp; buffer = tmp;
}
} }
} }
} }
@ -67,7 +68,11 @@ Stream IExtension.BeginQuery()
void IExtension.EndQuery(Stream stream) void IExtension.EndQuery(Stream stream)
{ {
using (stream) { } // just clean up // Clean up
if (stream != null)
{
stream.Dispose();
}
} }
} }
} }